Actually, many kinds of ants can’t walk upside down. It’s mostly the tree-dwelling ants who can.
Ants that live on trees tend to be excellent climbers. They have tiny, hooked claws at the ends of their feet which help them walk on the undersides of leaves and limbs. Much like a human rock climber uses a hook, ants use their claws to cling to jagged features of the surface.
What looks like a smooth surface to a human might be a craggy surface full of toeholds to an ant. But even ants would have a tough time walking upside down on a truly smooth surface.
Say you buried a glass jar in the ground with the lip flush with the ground’s surface. Ants would collect at the bottom, since they’d be unable to scale the walls.
Other common insects like cockroaches have no trouble walking upside down on truly smooth surfaces. Like ants, and most other insects, cockroaches have tiny claws at the ends of their feet. They also have special, sticky pads between their claws that can form a tight seal with a smooth surface. That’s why you don’t have to worry about cockroaches dropping down from the ceiling!