Why is space a vacuum? Also, why doesn't air from the Earth escape into space?

The typical speed of an air molecule is a few hundred meters per second, while escape velocity from Earth is over 10,000 meters per second. So almost all the air molecules just fall back down. They're affected by gravity just like everything else!

We do lose some air molecules this way, though. In particular, hydrogen and helium are lighter, so they move faster and escape significantly more often. This is why our atmosphere has very little of those two. But Jupiter, which has much stronger gravity, has an atmosphere mostly of hydrogen and helium.


Air fails to escape into space for the same reason you fail to: gravity. As noted in Kevin's answer, occasionally some do get going fast enough to escape. You would too, if enough stuff hit you hard enough. :)

Space is a vacuum (for some definition of vacuum), because vacuum is simply the absence of air/gas pressure, and there aren't enough gas molecules in space to create a noticeable pressure.

That said, note that there are particles in space; they are moving, and they do exert pressure on things they hit. See e.g. solar wind and heliopause. So depending on what/where you're measuring, space is far from a vacuum.


Gravity.

You can think of planets like wells or deep holes in the ground (gravity wells). Denser things fall to the bottom (rocks), less dense things rest on top of that (water), even less dense things on top of that (air), and finally the least dense thing on top of everything (vacuum).

The air, for the most part, isn't leaving the planet for the same reason the water isn't flying out of the ocean, gravity is holding it down.

It's important to remember that space doesn't suck. It's not that kind of vacuum. There is not a force pulling things into space in the same way there is not a force pulling air out of a hole in a balloon.