Well, no. The ideal gas equation tells us:
PV = nRT
thus pressure and volume vary with the number of moles of gas components and the temperature of the gas. "Space between the molecules" doesn't cause the pressure, though it does have a relationship with it. Decrease the volume and you increase the pressure of a given number of moles at a given temperature.
But change the temperature and the volume a gas occupies increases, so its density decreases. A gas can have the same pressure at a lower density by getting hotter.
Surface pressure is due to the weight of the air on top of a given area. At Earth's sea-level surface , on average, some 10.33 tons of air is weighing down on every square metre. In fact the pressure at every altitude in the atmosphere is principally caused by the weight of the air above it. Air gets compressed by that weight, so it gets denser the closer we get to the ground. So you're right about the molecules being closer together, but the pressure is from the weight of the air itself.
But why doesn't it escape? Any mass of gas at a given temperature has a certain amount of energy per unit mass. Temperature is actually a measure of the average energy of the particles of air. At the very top of the atmosphere, where molecules no longer collide with other molecules if they go straight up, then particles with sufficient energy can escape. There's usually a small probability that a fraction of particles will have enough energy to escape, but it's a very low probability. Only hydrogen atoms typically escape in any number because they're the lightest particles and require the least escape energy. This region of the atmosphere is called the exosphere and it is very, very tenuous. The number of particles at the top, in a given volume, is similar to the solar wind.