If space is a vacuum then why doesn't it suck in all the air from Earth's atmosphere? Actually, the answer to this is interesting.
While gravity is most certainly a real thing, the answer to this question is not “because gravity is stronger”. The actual answer is that the vacuum of space does not exert any force on the atmosphere at all. It does not “suck” the air. We associate the word “suck” with “vacuums” but it’s a misnomer. That’s not what vacuums do.
Consider an example where we’ve pumped all of the air out of some box and created a vacuum inside of it. Let’s say we’re on Earth, at sea level, and we poke a hole in the box. What will happen? Air will rush into the box and fill it. Okay. But why did it do that? Was it because the vacuum sucked the air into the box? No.
What’s actually happening there is that the air pressure around the box is forcing air into the space with no pressure. The air that fills the box is being pushed by air pressure into the empty space. Vacuums never “suck” air. What they do is present an empty space and then air pressure forces the air into the vacuum.
Air pressure is not uniform throughout the atmosphere. The lower the altitude, the higher the air pressure; thus, as you move higher, the air pressure decreases. In fact, at the upper limits of the atmosphere, the air pressure reduces to basically nil and since there’s no real air pressure to speak of up there, then there is no force pushing the air into the empty space.
Now, this doesn’t mean gravity isn’t at work here. In fact, gravity is the reason we have air pressure in the first place. Gravity pulls on the atmosphere, compressing it, and creating pressure. If we could magically turn off gravity then the atmosphere would be released from that compression and spring away from the Earth.
So, at the root of it, gravity is the thing that keeps the atmosphere near the surface. It just isn’t fighting the vacuum of space to do it.