A hat is designed as head-protection...from sun, rain etc. Clearly, it is unnecessary indoors. Apart from that, removing your hat is simply a mark of respect. That's why men used to raise their hats to passing ladies, young men would raise their hats to older men and all men would do so on entering a church. In the case of a uniform hat, the wearer - eg a policeman - is acknowledging by removing it that the house, office or whatever he has just entered is someone else's domain, not his.
In olden days, a knight's removal of his helmet would have been a signal that he was happy for his face to be seen and identified. It would also show that he was disarming himself by exposing his head to possible attack. Both of these were gestures of friendship.
In the case of women, there has been a longstanding tendency for them to cover their hair in public...consider the recent fuss about Muslim women and the hijab. When I was young - a long time ago! - it was unheard-of for a woman to enter a church without a head-covering...nowadays, the reverse is almost the case.