This expression alludes to the theatre.

It is an area unseen by the audience, where actors wait for their cue to come on stage. The wings are areas that are part of a stage deck but offstage (out of sight of the audience). The wing space is also used for storage of sets for scenery changes , props and as a backstage work area.

WAITING IN THE WINGS can also mean

Close by in the background; available at short notice, ready to do something or to be used at the appropriate time and to take the place of another person.
i.e.: My assistant is waiting in the wings to take my job once I retire.

Many actors have a nightmare close to a performance and the one below is no exception……………………

It is a scene that has often come into my mind, both sleeping and waking — I am standing in the wings of a theatre waiting for my cue to go onstage. As I stand there, I can hear the play proceeding, and suddenly it dawns on me that the lines I have learnt are not in this play at all but belong to quite a different one. Panic seizes me; I wonder frenziedly what I should do. Then I get my cue. Stumbling over the unfamiliar scenery, I make my way onto the stage, and then look for guidance to the prompter, whose head I can just see rising out of the floorboards. Alas he only signals helplessly to me, and I realise of course that his script is different from mine. I begin to speak my lines, but they are incomprehensible to the other actors and abhorrent to the audience, who begin to hiss and shout: “Get off the stage!” “Let the play go on!” “You’re interrupting!”

Malcolm Muggeridge

Thank goodness it is just a nightmare!