Many contained substances have a top. For example, canned goods and goods in jars have a top. The top prevents the contents from spilling out. A natural question can be asked about if the atmosphere has some sort of top the keeps the air contained in the atmosphere. Without a top it would seem the air would escape into outer space.

In one respect, the air does want to escape into outer space. There is higher pressure near the surface with extremely low pressure high in the atmosphere. The vertical pressure gradient force wants to force the atmosphere into space. It does not occur though because of gravity. Gravity balances the vertical pressure gradient force by forcing the air toward earth’s ground surface. This balance is called hydrostatic balance.

Some of the air does manage to escape into space. Lighter gases such as hydrogen have an easier time escaping since they are very light. Other gases in general can also escape near the outer boundary of the atmosphere where the atmosphere is merging into outer space. The amount of gas escaping though is relatively low and is replaced by gases that escape from the underground of earth and into the air such as at volcanoes and plate boundaries.

Thus, the atmosphere does not really have a top but rather ever so gradually merges into outer space. Air gets less and less dense with height. At about 600 km in height above the ground surface, the atmosphere is so thin that it has basically completed the process of merging into outer space.