Gravity is the key to planets maintaining an atmosphere. While most planetary objects contain an atmosphere, Earth's atmosphere is able to support life.
Early in the formation of Earth, our atmosphere was much different. It was likely composed of hot hydrogen and helium as that was what abundantly surrounded the sun in the early stages of the solar system. Because of the high temperature of the young Earth, this hydrogen and helium contained a lot of kinetic energy and escaped the gravitational pull of earth by exceeding the Earth's escape velocity.
Once volcanic activity began pumping heavier elements out above the surface of the Earth, our atmosphere was dominated by water vapor, CO2, and ammonia. The CO2 proceeded to dissolve into the oceans leading to the miracle of life.
Mars in comparison is only ~15% of the mass of Earth, so gravity is much weaker making it difficult to maintain an atmosphere which can support life, but there is an atmosphere on Mars none the less.
Venus is similar to Earth in size and mass, but due to intense volcanic activity, the atmosphere is comprised heavily of CO2 which led to a run away greenhouse effect and the intense, heavy atmosphere of Venus.
A planet like Mercury has essentially no atmosphere. It does have an extremely thin atmosphere, but most of the molecules which could make up the atmosphere of Mercury are blown away by solar winds and the lack of gravity on the planet.
Our moon is another example of extremely thin atmosphere.
So the key components to atmospheric formation are: mass/density of the planet (gravity), which determines the escape velocity, the composition of elements which can possibly make up the atmosphere, and the temperature of the atmosphere.