@phsicsboy,
It is gravity that keeps gas molecules close to the Earth surface. The atmosphere has varying pressures at different elevations due to gravity. The same effect is seen in bodies of water. The deeper you go the greater the pressure. Molecules of gas can be found hundreds of miles away from the surface of the Earth but out there the gas is very thin. So the gravity of the Earth is what is fighting the tendency of the vacuum to suck the gas away.
The seemingly random motions of molecules are what actually drives these particles to go into areas of lower pressures like a vacuum. The lower pressure areas are areas which have less molecules in them. So the molecules do not collide with each other and there is nothing to stop their inertia.