As I understand it, bees swarm for a couple of reasons. First, it's a means of expanding their population. Second, they may swarm because their living quarters are too cramped. A third phenomenon, which looks like a swarm, is really an absconsion, in which the whole colony leaves a hive it deems unsuitable.
The way it works is like this: Somehow the determination to swarm is reached about three weeks before the fact, usually in early to mid-spring. The workers start preparing several babies to be potential queens. Right before those queens hatch out, the old queen flies out of the hive, taking half the workers with her. They all gorge themselves with honey before they go, as fuel for the journey. (Click on the photo above to see close up how plump those bees are!)
The queen settles on a branch or other surface somewhere not far from the hive. The workers land all around her, forming a cluster in which they're all hanging on each other. They seem angry as they're circling and buzzing, but in fact they're super-mellow because of all the honey and the lack of anything to defend. It is possible to stick your hand into the middle a cluster very slowly and not be stung. (Or, like Ingo, above, you can put the queen in a little cage under your chin and create an awesome beard.)
From the cluster, scouts go out looking for a place to live, then come back and tell the others what they found. When a consensus is reached, they all fly off to their new home. The honey-laden bees start building comb, and the queen immediately begins laying eggs in it. They need to create enough babies ASAP to create enough workers to collect enough nectar and pollen to survive the next winter.
Back at the old hive, the first new queen to hatch out goes around to all the other queen cells and kills them, then flies out of the hive to mate. When she comes back, she starts laying eggs and spends the rest of her life (3-5 years) doing so.
Thanks to Beth for asking the question!