The small one in the first picture, although I guess it's technically a hat, we don't really look at it that way and it's quite normal to wear a regular sized hat (such as the following two) on top of the first one.
The first one is called a kipah. The Talmud links head covering to increased seriousness towards G-d. So a requirement developed to have the head covered. Different communities developed the requirement differently. Currently in Orthodox communities, it's generally accepted that it's required to be worn at all times.
The other hats are simply because wearing hats used to be accepted practice, and once it became part of the traditional dress, it wasn't altered. In communities that modernize and drop the traditional dress, you'll generally not find these hats.