Fighting in hockey is an often misunderstood, but necessary, element of the game. Hockey fights serve many diverse purposes in the game.
Many people who do not understand the concept of hockey tend to believe that fighting is a way for a team to take an opponent out of a game, but most often this is not the case.
Fighting in hockey is a bit like an animal urinating on a tree. Just as an animal will pee on a tree to mark its territory to show ownership, hockey players use fights to mark their territory and show ownership of the ice and control of the game. When a team shows that it is prepared to fight, and will win against, anyone who dares to challenge their ownership of an area that team has a strong advantage over the other.
Another purpose of fighting in hockey is for the team to protect their own players. When a team is trying to establish their presence on the ice they try to intimidate the offense (wings and center) of the other team. They do this through forceful checking and roughing the players up in general. It is common for a defenseman to stick up for their wingers and centers in a 'you mess with him, you mess with me' kind of mentality. If the other team 'messes with him' too much a defenseman will drop the gloves and go a round to defend his teammate.
Another purpose is to switch the momentum of the game. When one team is controlling the puck for a significant amount of time sometimes it is good to break that momentum with a fight. It's kind of like when a basketball team is on a hot streak and the other team takes a time out.
And of course another purpose is because it's just plain fun to watch!