I've been invited in some interview which is related and I want to bring with me legit knowledge of the history and culture of it.
The first thing to know is that, as late as the mid-19th century, everyone wore hats. It was considered good hygiene because it protected your hair.
Because of that, it was almost an extension of a person and there are a lot of rules for it. It is similar to shaking someone's hand (which also has a cool history). It was custom. It was a sort of "how-do-you-do?" when you tipped a hat at a lady, or in greeting. It was a sign of respect, like a salute, to remove your hat during something like the National Anthem. (Americans do that... Not sure if anyone else does.)
The reason it is removed indoors is basically because it isn't needed. The indoors, as a rule, is cleaner than the outside. That is also the reason you should keep it on when going through corridors, an elevator or when you're not going to sit somewhere- you are likely going to be outside again soon, so it is good to keep it on.
It is more of a traditional thing and a sign of respect than anything else.