There's really two reasons for alignment IMO. Here's both of them.
Out-of-game context: Alignment isn't supposed to be a straightjacket, but rather a statement about how you intend to play a character. When you play a lawful good character, you are saying that you intend to be a generally forthright, charitable fellow. You can occasionally indulge in a bit of non-lawful or non-good behavior, but as a player you're stating how you generally intend to act in game. The purpose of alignment for the players is to give themselves and the DM a relative barometer of what sorts of people their characters are.
In-game context: Alignment isn't just a label to the characters in D&D worlds- they are forces that drive the cosmos (PHB p. 103). You can detect them absolutely with spells and fight them more effectively with certain magical things (such as weapons with the holy property or a paladin's smite evil class feature). There is no real "purpose" for alignment for characters, just as there is no "purpose" for gravity, magnetism, heat or radiation. Alignment is simply another concrete, measurable force that exists within D&D worlds (albeit one that characters have some degree of control over). Some creatures are born or created with their alignments (such as demons, angels or slaadi). Some have cultural inclinations toward certain alignments (such as elves or orcs). But for must people, alignment is something that you either gradually realize about yourself or consciously decide to work towards.
I've always imagined that D&D characters probably have similar debates as to the meanings of alignment as players do (within the context of their knowledge). Obviously, good characters don't consider detecting "evil" to be a carte blanche to maim and destroy. Book of Exalted Deeds goes into more detail as to what it means to be good in the D&D sense, just as Book of Vile Darkness does for evil.
In short, the ultimate purpose of alignment is for the DM to communicate to the players about what sort of game he wants to play. For players, it's just another way for them to say who they are. The confusing part is the game mechanics based on it. The game is deliberately designed to be vague as to what actions result in what alignments- this is supposed to be one of the things the DM gets to decide for himself. Some DMs tell their paladin players that they can't use poison, attack unarmed enemies, or refuse honroable surrenders. That sort of thing is for each individual DM to decide.