To add to @Robusto's answer - regarding the origin - the following seems more definitive and is taken from "The Facts on File dictionary of clichés"
To be extremely tired or about to
collapse; near the end. Despite the
implication, this term never meant
that legs were in any way serial—that
is, beginning with the first and
ending with the last. Rather, it uses
last meaning “near the end” (of one’s
energy or life). The expression was
already used in the sixteenth century;
it appears in the play The Old Law
(1599) by Thomas Middleton and Philip
Massinger: “My husband goes upon his
last hour now—on his last legs, I am
sure.” In John Ray’s Proverbs (1678)
the term is defined as meaning
“bankrupt,” and since then it has been
transferred to anything nearing its
end or about to fail, as in, “This
cliché may be on its last legs.”
However this link dates "The Old Law" as
On his last legs. The Old Law
(1618-19), Act v. Sc. 1.
The exact text as it appears online
EUGENIA My husband goes upon his last
hour now.
FIRST COURTIER On his last legs, I'm
sure.
EUGENIA September the seventeenth, I
will not bate an hour on it; and
tomorrow His latest hour's expired.
The 1811 Dictionary in the Vulgar Tongue, by Francis Grose defines shag as:
To SHAG. To copulate. He is but bad shag; he is no able woman's
man.
In the same dictionary there is the seemingly unrelated term church warden which Grouse writes:
A Sussex name fora shag, or cormorant, probably from its voracity.
Cormorant and shag is the name of a type of seabird, the terms are mostly interchangeable although ornithologists will make the distinction between the Great Cormorant, a British seabird, and the European Shag (also present along the UK coastline) so-called due to the appearance of a crest during the mating season. The European Shag is well-known for its voracious appetite and is capable of diving at least 45 metres for anything up to 45 seconds in order to catch its prey.
Shags are opportunists, feeding on a wide range of benthic, demersal
and schooling, pelagic fish. Source
-----------
For info on the European Shag see: Sexual ornament size and breeding performance in female and male European Shags
and Wikipedia.
The Oxford Concise English Dictionary dates the term shag from C16th
Shag n. a cormorant with greenish-black plumage and a long curly crest in the breeding season. [Phalacrocorax aristotelis.]
Origin C16: perh. a use of 'shag', with ref. to the bird's 'shaggy'
crest.
Oxford Dictionaries tells us that sexual meaning of shag could be derived from Old English sceacan for shake and quake.
Shag as in shag pile carpet was sceacga ‘rough matted hair’ in Old English. Of Germanic origin, it is related to Old Norse skegg ‘beard’. The mid 16th-century bird name shag is perhaps a specific use of the word, with reference to the bird's ‘shaggy’ crest. The use of the verb to mean sexual intercourse is first recorded in 1788, but is probably older. It may go back to an older verb meaning ‘shake’ (which may be the origin of shag) which is recorded between the 14th and 16th centuries.
When one partner energetically shags another, it's certainly true that their bodies toss and turn, moving to and fro rapidly, either one may experience a trembling sensation, and the orgiastic line: Did the earth move for you? may be uttered. Although that particular expression was coined by Ernest Hemmingway in 1940, the concept of something being so forceful, so powerful that it causes the Earth to shake is as old as time immemorial.
Americans were “shagging” before 1999
I've been watching the entire 11 seasons of Cheers, 1982 to 1993. As luck would have it, I've come across shag being used for the first time in the hit TV sitcom show; season 11, episode 25, The Guy Can't Help It, which aired May 13, 1993. Now I've heard the term boink being uttered a couple of times in the show, especially by Carla, the bar's waitress, but never shag. Not until this episode: Sam Malone, the charismatic and highly-sexed owner of Cheers, a Boston bar, has reluctantly agreed to see a sex therapist. In the first group therapy he attends he admits
Sam Malone 19.24: I don't know what the big deal is. You know, when I was growing up, it was a sign of manhood how many women you shagged. I guess I just missed the announcement when they changed the rule. So I just, kept on doing it.
Best Answer
The idea seems to be that your feet become hot with walking and that when you stop walking they, and in particular, your heels, cool down. Hence, the current meaning of having to wait. The earliest recorded use of cooling the feet in this way is dated 1576. Coole their heeles first appears in 1606, where it appears to refer to horses. It is first applied to people in Chapman’s translation of the Iliad published a few years later.