This expression comes from the theatre in reference to an actor studying his part in the wings (the areas to either side of the stage). Such an actor may find himself or herself suddenly called on the stage to replace another actor slated to be on stage or currently on stage who cannot complete his or her role.
The term was eventually extended to other kinds of improvisation based on unpreparedness including prompters who fed lines to entertainers on stage who had either forgotten their lines or who did not know them well enough in the first place but who found themselves on stage anyway.
In Philip Godfrey’s 1933 book “Back-stage: A Survey of the Contemporary English Theatre From Behind the Scenes” the author wrote:
“He must give a performance by ‘winging it‘ – that is, by refreshing his memory for each scene in the wings before he goes on to play it.”
By the mid-1900s, the phrase meant any performance — prepared or not — where improvisation takes the lead and all else (and everyone else) follows in the hopes that they will get to the anticipated destination or goal.