Gee, that's the best question I've heard in 6 months!!!
It's an old English custom where hats are tipped, (or doffed) slightly lifting the hat off your forehead, when meeting a lady (remove your hat if you stop to talk), or to "say" to anyone, male or female – thank you, excuse me, hello, goodbye, you’re welcome or how do you do.
Tipping of the hat is a conventional gesture of politeness. This hat tipping custom has the same origin as military saluting, which came from the raising of medieval Knights face visors to show friendliness.
Hats are worn less now, but at the turn of the 20th century, all adults wore hats whenever they left the house. It was a matter of good personal hygiene, since hats were a protection from industrial dirt.
Hats are removed when inside, except for places that are akin to public streets, like lobbies, corridors, and crowded elevators (non-residential). In a public building (where there are no apartments) the elevator is considered a public area.
You may choose to remove your hat in a public elevator, but in the presence of a lady your hat must be removed.
A gentleman takes off his hat and holds it in his hand when a lady enters the elevator in any building that can be classified as a dwelling such as an apartment house or hotel. He puts it on again in the corridor.
It's therefore become a mark of respect to take them off during a meal, funerals, church service, when you greet someone in the street, or any other occasion when you want to signify that you respect the person. That's where the term, "I take my hat off to you" originated.
Thanks again for a really good question! I learnt something tonight. The website is very interesting and took me ages to find.