My (admittedly imperfect) understanding is that this happens when a new queen is formed in a hive - the bees split into two groups, some with the old queen, and a bunch with the new queen as they run out looking for a new home.
So that's enough bees of different functions to bootstrap a new hive when they find an appropriate cavity to inhabit. This group will have a hard time, I expect.
yep, when a hive is over crowded the hive workers decide to make new queens by feeding selected larvae exclusively royal jelly, which in turn creates queen bees. The hive makes several new queen larvae simultaneously. They are in special cells that look like peanuts and have more room to develop.
The queen pupae start to make a sound in the cells to alert the hive they are nearing maturity. The old queen will then bugger off and look for a new home with many if not most of the worker bees. The new queens fight to the death so that (hopefully) only one queen is left. But all the new queens can end up dead.
If there are enough brood cells developing the old hive can continue to thrive with a new queen, but sometimes they may not have enough resources. Same for the new hive, they may perish and not find a new home and food. Usually the swarms occur in early summer when resources are plentiful but not always.
Beekeepers need to watch their hives closely, and if too crowded they need to either add another brood box to the hive, or split the hive to prevent a swarm. To split a hive they need to add a queen, or a queen cell that will hatch out within a few days. The bees who are moved to the new hive with enough brood, honey and pollen frames, and the new queen cell will go on as if nothing happened, ditto for the old hive and the old queen.
Of course I've simplified the heck out of this short explanation