Since the dawn of humankind, we have been wearing hats. They have always functioned as powerful markers of status, sex, occupation, class and origin. Whether you are a king, soldier or shaman, they defined you.
Each hat often had a set of rules on how it is worn, when and by whom. These rules extended into all headwear, most notably in terms of when you should remove your hat.
The most common practice was in the tipping of your hat. Either fully removing it, or lifting it slightly at the front. It was always done as a mark of respect for the person, or activity you are engaged in. Typically hats were removed when entering holy places, homes or in the presence of your superiors.
The rules were very detailed, often legally enforced, and varied greatly by location and time. Everyone knew these rules and would vary them only as a profound social comment. If you think of the military, parliament, church, workplace, you can see how it was all tied to social hierarchy.
As a woman, there was another completely different set of rules. It was very rare that a woman would need to remove her hat - probably because of the elaborate pins and stays that were used to hold the enormous hats in fashion. All the rules reeked of racism, elitism and sexism - but it was a different world.
Since the 1960's all these rules, have faded away, and people often wonder what they should do. Personally, I think you should do whatever you feel like.
If you want to acknowledge someone, or show respect, then tip your hat. It is a gracious thing to do. If you are entering a place you revere, then removing your hat is a gentlemanly way of showing your respect. In much the same way as shaking hands, it is an act of openness and regard. Dealing with any old-world authority, such as courts and churches, usually requires the removal of your hat, but only if you are a man.
I wear my hats from morning until night, and rarely take them off except when I go to bed. No one has ever minded, and honestly, no one even remembers what the rules are. I will often tip my hat to other hat wearers, or when someone has done something I admire. Holding your hat over your heart shows your profound respect.
In France, they call 'chapeau' when something is to be celebrated. In much the same way as throwing your hat in the air.
Hats have changed. There are no longer any rules, but by tipping your hat, you have a potent gesture that you can wield thoughtfully. It feels wonderful and can make someone's day.