I heard the expression being handed your hat being used to mean that you are invited to leave. What is its origin and what are the possible variations?
The Phrase Finder has the following excellent summary of this phrase's origin, which goes so far as to include a couple variations:
Origin: in more ceremonious days, if someone came to visit you, you would see him to the door at visit's end. Then either you or your butler would hand him his hat, since everyone in those days wore hats, but not inside. Figurative meaning: to be shown the door, to be sent packing. Can be used to mean to be fired.
You can use it in a variety of ways, such as: "He expected another term, but the voters handed him his hat." Or, "I was confident that my actions were for the good of the company, but I was handed my hat anyway." Or: "I offered him first crack at my new invention, the one that would save him thousands annually. I didn't expect to be handed my hat and shown the door."
Of course, the literal meaning is still valid. If you are looking around for your hat, someone is likely to hand it to you in simple politeness.
"Was handed his hat" gets 123 results on Google; using "her" gets 33, "their" gets 14, "being handed his hat" has 97, etc. I'd say it receives a respectable amount of attention, by no means ostentatious. To compare, "was shown the door" gets 560 results.
The first three instances of "handed him his hat" that a Google Books search for the years 1700 through 2000 finds are from 1843, 1845, and 1846. Here they are, in context. From Ben Bradshawe: The Man Without a Head (1843):
Sophy had stuck it [Ben's hat] upon a bust of Shakespeare which adorned the hall, but as Ben never dreamt of having such a head in his hat he had overlooked it. Sophy went into the hall and handed him his hat ; but Ben, marvelling at the atrocity of his own impudence took the hand as well as the hat, and shook it till the old hat trembled more than he ; then seizing the hat he pressed the place where the hand had been to his lips, and hurried from the house, leaving both sisters in a state of unutterable confusion.
From a translation of Johann Jung-Stilling, Theobald, or the Fanatic: A True History (1845):
Upon this the fire began to burn in the bosom of Theobald ; he approached the teacher, and asked him if, as a gentleman, he were acquainted with the rules of good breeding? Here both of us are invited as guests, and are on an equal footing ; now sir, you must leave the house immediately. He then handed him his hat and cane, and showed him the door, then taking his own hat and cane, followed right after. A couple of persons followed them under the apprehension that they might come to blows ; but Theobald had no such intention ; he went quietly home, and so did the teacher.
And from "Hochelaga," in Blackwood's Edinburgh Magazine (October 1846):
An Englishman at the New York theatre, having engaged, paid for, and established himself in a snug front corner of a box, thought himself justified in retaining it, even when summoned by an American to yield it to a lady. A discussion ensued. The pit inquired its cause ; the lady's companion stepped forward and said, "There is an Englishman here who will not give up his place to a lady." Whereupon the indignant pit swarmed up into the box, gently seized the offender, and carried him out of the theatre, neither regarding nor retaliating his kicks, blows, and curses, set him carefully down upon the steps, handed him his hat, his opera-glass, and the price of his ticket, and shut the door in his face.
All three of these instances involve a departure—in the first case initiated by the person who receives his hat, in the other two by a person or persons anxious to see the recipient out of the building. It should be clear from the second and third examples that being handed your hat can serve as a far-from-oblique hint to quit the premises.
One odd variant that I've noticed in recent years involves being "handed your head." From Clayton Crockett, Religion and Violence in a Secular World: Toward a New Political Theology (2006):
We modern democrats congratulate ourselves on having revolutionized this schema, having turned it upside down, by ridding it of its top-down power structure. We have shown the king the door (or even handed him his head) and replaced him with a constitutional democracy, according to which power rises from the bottom up.
The sense of the variant expression is "chopped off his head and handed it back to him [sometimes, on a platter]"—not a very logical way to show someone the door, perhaps, but quite vivid.
The ealiest instance of this form of the expression that a Google Books search discloses is from Israel Magazine, volume 2 (1970) [combined snippets], where the meaning seems less metaphorical than it usually is in later instances:
He [the Arab complainant] thinks the terrorists may find out, accuse him of recognizing Israel's juri[s]diction over East Jerusalem, and hand him his head. When Kaddar believes that the man's fear is real he will make an exception to the rule that the Ombudsman can consider only written, signed complaints, a rule found to be a necessary precaution against malicious defamation and slander, which some complainants have tried.
The next-earliest instance in the same search is from John Leonard, Black Conceit (1973) [combined snippets]:
"What did Louis do to him when Boileau dumped on his sonnet?"
"Nothing? You'd expect a guy like Louis to hand him his head."
"Yes, well." The pedagogue sprang to attention. "Those were the days when poets were taken seriously, even by kings. Louis XIV would accept the judgment of a well-known poet on his own stab at poetry, because Louis granted a certain kind of authority to the opinion of an expert in that field. ..."
Here again, the suggestion of head-handing has a connection to some actual danger of beheading. Such is not the case with the occurrence in Shirly Tuska & Joseph Jenks, Teacher Personalities: A Mirror of Self (1974) [combined snippets]:
I came to college to keep out of the draft. At first it was a drag. The last couple of years have been pretty interesting, and I'm thinking of being a teacher. I'll tell you one thing though. If any little creep tries messing up my class I'll hand him his head!
The two expressions thus seem somewhat different in sense: "hand [a person] [his or her] hat" means to unceremoniously dismiss the person, and the closest matches for it among idiomatic expressions are probably (as Daniel notes in his answer) "send [someone] packing" and "show [someone] the door; "hand [a person] [his or her] head" can have the overlapping meaning of getting rid of someone, but the sense of the expressions is more aggressive and (at least metaphorically) violent, and the closest idiomatic expressions are probably "bite [someone's] head off" or "have [someone's] head." In the context of firing someone, you might say either "handed [the person] [his or her] hat" or "handed [the person] [his or her] head"‚or you might use a third expression such as "gave [the person] [his or her] walking papers."
In more modern terms... Many folks wear baseball hats. Being handed your hat would be the result if you were involved in the losing end of an altercation. If someone punches you hard enough in the face it will usually knock you to the ground quite naturally your hat will be knocked off in that process. Hence, having your hat handed to you as you're getting up dazed, now you can leave cause you just been beat!
"You have been handed your hat." So to speak terms means " you have been shown the door" but that door may have a better opportunity on the other side that you were not exspecting.
Optimism is a good way of looking at it.