Throughout most of the world during the 1600s, most men wore hats all the time.
They would wear them inside their own home, at meals, in bed, outside, and on any important occasion. There is no exact record of the date when it began to be thought impolite to do some of these things.
Ever since people first started to wear hats, they have used them for other reasons than just to keep their heads warm. Long, long ago, when soldiers wore armor, the less important soldiers always had to take off their iron helmets when they were with someone more important.
Later, knights of equal rank took off their hats when they were with one another just as a sign of respect. No one, of course, was allowed to wear anything on his head when he stood in front of a king.
All of these habits are how the idea of taking off your hat, or just tipping it, became a symbol of showing respect to someone.