The answer in a word is...
Gravity
Fortunately for us, Earth’s gravity is strong enough to hold onto its atmosphere. Mars, for example, is less than half Earth’s size and around one-tenth Earth’s mass. Less mass means less gravitational pull. Mars’ atmosphere is only about 1/100th as dense as Earth’s. And, by the way, it is mostly CO2.
The air at the bottom of the atmosphere is under a lot more weight than the air nearer the top.Like the acrobat at the bottom of a stack of acrobats, the air at the bottom of the atmosphere is under a lot more weight than the air nearer the top. That means, the air nearer Earth’s surface is squished by the air above it, and is thus denser. The higher you go in the atmosphere, the thinner the air becomes. Ninety-nine percent of the air is in the lowest 30 kilometers (19 miles) of the atmosphere.
Yes, Earth’s atmosphere has weight. So we, here at the surface, at the bottom of the “stack,” have about 14.7 pounds of air pressing down on every square inch of our bodies! Fortunately, we’re used to it. We evolved down here, so our bodies can handle it. Higher in the atmosphere we begin to have problems. Even at 3,000 — 4,500 meters (around 10 — 15,000 feet) altitude, the air becomes thin enough that most people have trouble getting enough oxygen.
If Earth were the size of a beach ball, the breathable atmosphere would be as thin as paper. Seeing our atmosphere from space shows us how thin and fragile it is.
"Many astronauts have reported seeing that delicate, thin blue aura at the horizon of the daylit hemisphere - that represents the thickness of the entire atmosphere - and immediately, unbidden, contemplating its fragility and vulnerability. They worry about it. They have reason to worry.”