Hat trick" originated from the English game of Cricket. The term originally referred to a bowler retiring three consecutive batsman with three consecutive balls.
This is roughly equivalent to a pitcher in baseball striking out three consecutive batters using only three pitches to each! This was considered quite an accomplishment and was traditionally rewarded with a hat.
The term is now used for other sports, always referring to an accomplishment of three. A popular use today is three goals by a single player in one game of hockey or soccer.