Technically speaking, every planet has an atmosphere (though most drop Mercury from the list because it's so thin). Let's start with the big guys, which are pretty straightforward. Gas giants just keep their atmosphere from when they gathered the remnants of gas from the nebula that their parent star formed from. If you're like the gas giants in our solar system, you usually keep this atmosphere for a very long time because you have a strong magnetosphere and you also are fairly far from your star. Your star also isn't particularly powerful. Some exoplanets are categorized as hot Jupiters because they are similar to jupiter in composition but are orbiting very closely to their star. If this is the case, despite the strong magnetosphere, their star may quite literally boil their atmosphere off, either adding it to the star's mass, or blowing it away, sort of like a comet. Eventually they may become what we call chthonian planets, which are hypothetical planets that are the rocky cores left over from boiled gas giants.
How about rocky planets like Earth? They get their atmosphere from a process called outgassing, which basically means that volcanoes burp out gases. Earth is unique in that we believe our atmosphere was at least partially created from comets and wet asteroids. Venus burped out too much greenhouse gases and got a hellhole for an atmosphere. At least it rains there...rains sulfuric acid...and even that boils before it reaches the surface! Mars probably had a thicker atmosphere than today, as evidenced by big volcanoes like Olympus Mons. What happened to it? Well first, Mars doesn't have a very strong magnetosphere because it is simply smaller than the Earth. Because it's smaller its core cooled down faster, and its mantle no longer supported a good magnetic field. More charged particles means a higher chance of atmospheric stripping, which Mars suffered from at least partially. However, we think that a large impact probably knocked a lot of Mars's atmosphere off. Finally, that leaves us with Mercury. Mercury is pretty small and very close to the Sun. It just can't retain an atmosphere because its gravity is weak. The same can be said for most moons in our solar system, as they are typically smaller than Mercury. They are simply too small to retain an atmosphere.
Wait, then how come some moons have atmospheres and others don't? Well the ones that don't, as I said before, are too small to retain one, and they also have to deal with interactions with the solar wind, atmospheric stripping, and interactions with their planet. Some moons have volcanic activity, so their thin atmosphere can be replenished with burped up gases. In fact, some may have weather! I believe Io and Triton both have weather systems, despite having very thin atmospheres. Titan has the thickest atmosphere of the bunch, with one that is about 50% denser than Earth's. Thus, it also has complex weather systems. But why? Titan is similar to Ganymede in structure. Why does Titan get to have an atmosphere? Short answer: we don't fully know why. We think it might've been comets, but we really aren't 100% sure on anything at this point. Go check out the wikipedia article for more.
So to answer your question, atmospheres are usually formed from volcanic activity or, for gas giants, retained gas from nebula remnants. Atmospheres are lost due to low gravity, high temperatures, solar wind, atmospheric stripping, large impacts, and planet-moon interactions.