Neil Steinberg has a good book about this (Hatless Jack). Hats have definitely been going out of fashion in North America, and most of the Western world, for the last seventy-five years or so. Part of it is that they're less necessary nowadays as people rely on cars and public transportation. Part of it is about how hats were seen as stodgy and conformist at some point, and post-WWII fashion valued the young rebel. Part of it is that fashion has become less and less formal, so only the most informal forms of headwear remain (beanies, baseball caps, etc).
Really, the story isn't that different from ties and tailored clothing. Sales are maybe a fraction of what they used to be fifty or seventy-five years ago across the board. You have to really hunt nowadays to find a hat store, whereas that wasn't the case in the immediate post-war years. (Same with any kind of "formal" clothing). Arguments about how hats are coming back are silly. This is a blip in a longer story about fashion trends. Just like how there's been a small uptick for tailored clothing, even if the broader trend has been an obvious decline.
Agree with @Andy57
above, however -- this doesn't mean you can't look great in one, just as you can look great in ties and sport coats. Hats nowadays are more elective nowadays. They're a style statement, whereas they used to be just about social norms, etiquette, and matters of practicality (kind of like the story of all tailored clothing, really). Older guys seem to have an easier time pulling off hats, but I think it's possible for young guys to wear them as well. See @NickPollica
at Eidos, Ethan at Bryceland's, Tony Sylvester, @shoreman1782
, @conceptual 4est
Like with anything that's exciting in clothing, I think at some point, you have to say you don't care and dress for yourself. Yes, you'll stand out a bit more with a hat, but if your goal is to just conform and blend in with passable, decent-looking clothes, you'll just wind up with the sort of vanilla bizcaz stuff sold at J. Crew or whatever.