The awarding of "hats" is a temporal activity carried out on the SE network around the change of the years, known as Winter Bash; by mid January all hats are gone. Very likely next December you can get new hats; the old stay still gone.
For the most part "hats" are like "badges," that is one gets them automatically (see note at the end, though) for some specific action carried out on the site.
A difference is that some of these actions are more unusual and/or time-bound than those for the badges.
For various hats what one needs to do in order to get them is described on the site of the event (see link above). In addition to these regular hats there are secret hats and those become only apparent during the event.
Somebody serendipitously (or intentionally trying things out, informed by earlier experience) triggers a secret hat and then people try to figure out how to do this. You can follow this activity in a dedicated thread on the main meta, already provided in a comment: Winter Bash 2016 - All Hats
And this guessing and searching the secret hats, is in a way a main part of the event. In recent years there even was a hat awarded for correctly guessing what triggers a secret hat (this one was manually awarded by an SE employee in charge of the event).