This phrase, meaning to die or stop working, is probably a lot older than you think. A famous variation on the line is in the King James translation of the Bible from 1611:
Jesus, when he had cried again with a loud voice, yielded up the ghost.
It might seem odd to think of a ghost in relation to Jesus, considering that in modern English ghost almost always refers to the disembodied spirit of a dead person, and is generally imagined as haunting the living.
In the past though, ghost was synonymous with spirit or soul, even of those still living. This is why the origins of the phrase go back at least to Old English, as the concept of an immaterial soul is incredibly old. We can still see the effects of this older meaning of ghost in the Christian figure known interchangeably as the Holy Ghost or the Holy Spirit. Or in the German loanword zeitgeist, meaning spirit of the age, but perhaps more literally translated as the ghost of the age (just as poltergeist means noisy ghost).
To give up the ghost then meant to allow your soul to leave your body, as you give it up to a higher power. And if you think about it, this isn’t very different from how we use to give up in general.
To give up has two basic meanings: to surrender, or to stop doing something, such as a bad habit. The latter sense is the most similar to the meaning of to give up the ghost, as both generally involve stopping. More specifically though, to give up something meant to give it to a higher power. Christians, for example, giving something up for Lent, were giving it to God instead of having it for themselves.
To give up as in to surrender seems quite different, but still has a basic similarity. If you give up in a competition, or to some enemy, you’re putting yourself in their power, like you’re giving yourself to them, who are in a position of power over you.
If you know French, this might not surprise you too much. The verb to surrender comes directly from the French words sur (on/up) and rendre (to give). To give up therefore most likely came to mean to surrender as a direct translation of the French phrase.
The next time you feel like you can’t figure out the origins of an unusual expression: don’t give up!