The term "hat trick" is used when a single player achieves three scores in a single game. Its first recorded usage was after a game of cricket when bowler H.H. Stephenson took three consecutive wickets and was awarded a hat. The term migrated to other sports, such as football and ice hockey.
Stephenson was playing for an all-England squad against a team from Hallam, South Yorkshire in 1858. He hit the three wooden stakes behind the batter three consecutive times, and money was collected to honor his achievement. With the cash collected, a hat was bought which was then presented to the bowler.
It's not clear when the phrase began to be used in ice hockey or other sports. The Hockey Hall of Fame says it was first used in ice hockey when, in 1946, Chicago Blackhawks winger Alex Kaleta wanted to buy a gray fedora from a haberdashery. The owner of the shop, Sammy Taft, said the player could have the hat for free if he scored three goals that night. Kaleta scored four goals that game as Chicago lost 6 to 5 against the Toronto Maple Leafs. Taft then began giving out hats to players who scored three goals and the phrase caught on from there.