The fly, when walking, alternately moves the front and rear legs of one side of the body and the central one of the opposite side, then the other three. When moving over a rough surface, use a pair of nails that are at the ends of the legs: they allow it to adhere to the support and make it easier to move. When instead it rests or walks on a perfectly smooth surface, like that of glass, it uses a kind of finger placed between the two nails, capable of adhering to the glass itself like a suction cup. That's why it doesn't slip.
It must also be said, however, that for small animals such as flies, and therefore very light, the weight of the body is not so important in balance: the ability of the legs to adhere to the surfaces they walk on counts more.