Normally, a honeybee colony has a single, mature queen in it. That queen actively lays eggs. Worker bees tend to the larvae hatching out of the eggs until they seal the cell these larva are in and let them transform into an adult bee.
There are three types of honeybees - drones (males) that hatch out of unfertilized eggs, and workers+queens, both of which are females and come out of fertilized eggs. What separates the workers from the queens is the way they are raised - and here's the explanation.
There are three situations in which bees "decide" to raise a new queen.
1. When they feel their existing queen is getting old/failing, and they want to supersede her with a new queen.
2. When the queen has gone missing - died, wandered off the hive, whatever - but she's not in the hive.
3. The bees "decide" to swarm, i.e. take one hive and split it into two.
In all of these situations, the bees need a new queen. They choose female eggs (apparently at random) and relocate them into bigger cells ("queen cells"). These female larvae are fed richer food than the worker larvae (to be specific - in the first three days of their lives, worker larvae and queen larvae are both fed the rich royal jelly, but only queen larvae are kept on it, allowing them to mature faster and become queens). The cells are then capped, the larvae create pupae and transforms into queens.
The first virgin queen to emerge communicates with the other ones through piping - they create vibrations in the comb. The roaming virgin queen creates a sound called "tooting," its sisters answer it with "quacking." The free-roaming queen then goes cell-by-cell and kills them all.
What's next for the newbee queen is a bit of maturing in the hive for a few days, some practice flights to learn the hive's location, and then a flight to the nearest drone congregation area for the queen's only romantic adventure of her life, in which she'll mate with multiple drones from other hives - these drones will die on the spot.
Upon returning to the hive, what happens next varies by the reason the queen was created in the first place.
If it was for swarming purposes, its mother will leave with half the workforce. They will find a new spot for a hive, the workers will create the new comb there, and the queen-mother will start laying, while in the old hive her daughter will start laying eggs within a few days.
If the old queen went missing or dead, then now the bees have a new queen and a reason to celebrate - the hive was saved (no queen - the colony's days are numbered. When a virgin queen is around or when no queen is around, worker bees might start laying eggs, but as they have not mated, these will all be drone eggs. Male bees, much like human males, aren't good for much).
If the old queen was just getting too old for all of this, the new queen and/or the workers may decide to kill her. Alternatively, the new queen and the old queen may live in harmony for some time, both actively laying eggs, until the old queen expires (naturally or with some help from her formerly loyal subjects).
I guess the bottom line for your question - how do they choose a queen - the answer is "rather randomly."