Some planets only have an exosphere. The Space Shuttle, some communications satellites, etc. The drag from this small amount of atmosphere effects the orbits. Different answers for terrestrial and gas planets. Most important source of atmosphere is outgassing by volcanic eruptions.
Eruptions release trapped gas below crust.
Gases trapped by high pressure in rocks, only released when molten rocks brought to surface and gas could escape. Atmospheres aren't here to stay. Any atmosphere is constantly changing. The more massive the planet, the higher the gravity, so the higher the escape velocity is for this planet. Light particles move faster at a given temperature than more massive particles so hydrogen and helium can reach escape velocity more easily than oxygen.