I have had an issue with this one for awhile now. Apparently this was a real no no for my parent's generation. I don't understand it and I think it makes no sense. How does what you have on your head show respect or disrespect? Like you said, if it is something that may be offensive, like promoting something illegal, then as a courtesy to others you should take it off in a house of worship where they don't condone it.
I managed the restaurant at a golf course. We hard a bar area and a dining room. The dining room was rarely used by the golfers because they felt uncomfortable in the fancy room with chandeliers although it was perfectly fine for them to be there. One day a group was seated in the dining room because there was no room in the bar. As I walked up to greet them, one of the golf course employees walked through the dining room and up to a customer and said, "Sir, you need to remove you hat in here."
I was so mad. First of all he didn't have the authority to ask anyone to do anything in my dining room, he had no authority and second of all it was a freaking golf course. We sold golf hats. My mom is always going on and on about how she doesn't understand why my brother in law and my cousin's husband have to wear a ball cap everywhere they go. They are both balding so I suspect it makes them feel more confident. Who the heck cares? But for some reason it drives her crazy.
I am a pretty conservative person, but really why do we try to get in everyone else business on stupid things like wearing a hat?