Flies land on the ceiling by flying close to it, sticking their front pair of legs above and in front of their heads (a la Superman), and grabbing onto the ceiling as they fly under it. Their inertia causes them to do a half-backflip, pivoting around their front legs. They then grab hold with their middle and back legs.
This description was given in The Secret House, by David Bodanis. (now out of print, regrettably.)