Going back to ancient times, men removed their helmets indoors or in front of the King as a show of vulnerability and respect. Taking off your helmet meant that you could be easily killed, so doing this in someone else’s presence was basically a gesture of trust that they would not kill you, or perhaps in the case of appearing before the King, an admission that he had the authority and could kill you if he wanted to. This carried over through the centuries into a man removing his hat as a gesture of respect. Edited to add, I forgot to mention that in church specifically, there was an expectation that no violence would occur (this is why the incidents in medieval history where someone was killed in a church are considered so horrible) so another reason for a man to take off his helmet there was because he had an expectation of being safe while in the church.
Women on the other hand had the opposite tradition - they kept their head and hair covered and only showed it in the private presence of husband and family, as a gesture of modesty. Therefore, it became a gesture of respect for a woman to have some type of head covering such as a veil, a hat, a hair net type thing, etc., even if it didn’t fully cover her hair.