In terms of weight in comparison to volume, flies don’t weigh much, so very little force is required to keep a fly from falling.
That force is exerted by structures on the tarsi, the tips of the fly’s legs.
First, there is a set of claws, which can be seen with a very good hand magnifier.
There are also spongy pads that have ridges like a ruffled potato chip, providing for greater contact or adhesion.
They are cushions, not suction cups.
These body parts allow the fly to stay in place and move with confidence. When the fly moves, two of its six legs can be out of contact with the surface at any time.
Ants tend to rely more heavily on their claws, which are relatively larger than those of flies, than their pads, which are relatively smaller.
Some are not as good at climbing as you might think.
Ants that live on the ground where the soil is rough might not be able to climb well on smooth surfaces or upside down, the way tree-dwelling ants do.
The ones that get into houses, of course, usually have that ability.
They are very small, so they have even less of a weight problem than flies.