Hats are purely cosmetic and are just something people like to collect once they've gotten all the weapons they want. However, there is an exception to this rule, and they are called set hats which came from a contest from the website Polycount. These five hats when worn with certain weapons give the player an added bonus. Valve has acknowledged this as a mistake and has released new sets without hats but the old ones remain, likely because people payed money for them.
For more information on set hats and the game itself I suggest the
official wiki or
add me to friends if you'd rather just ask a quick question.
PS: :crying: