It comes from a famous American World War Two patriotic song, with words by Harold Adamson and music by Jimmy McHugh. It tells the tale of a plane struggling home after a bombing raid:
Comin’ in on a wing and a prayer
What a show, what a fight
Boys, we really hit our target for tonight
How we sing as we limp through the air
Look below, there’s our field over there
Though there’s one motor gone
We can still carry on
Comin’ in on a wing and a prayer.
The song came out at the end of 1942 and instantly became a huge hit on both sides of the Atlantic, so much so that the phrase almost immediately entered the language. (Source: World Wide Words)

