During the need to swarm phase, the queen lays eggs in queen cells the workers are building for the new queen.
When they go queenless, I would guess they pick larvae at the right age and in an area that can be made into a queen cell easily, but I am guessing here.