Space has a lot of stuff so how can it be a vacuum?
Vacuum does not mean a space that is completely devoid of matter, dark matter, or photons. While a perfect vacuum makes for a very nice spherical cow assumption, no such space exists. While physicists do like their spherical cow assumptions, they also self-effacingly deride themselves for making those spherical cow assumptions.
There is no such a thing anywhere in the universe as a perfect vacuum. However, the assumption of a perfect vacuum does yield useful results for many electromagnetic interactions. An imperfect vacuum -- now that is something that is encountered in space, and even in laboratories on the surface of the Earth. The best vacuum attainable in a lab is nowhere near what is attainable in space, even near-Earth space. For example, low Earth orbit space is considered to be both vacuum (it's a much himgher quality vacuum than any vacuum attainable in an Earth-bound vacuum chamber) and yet still in the atmosphere.
Space isn't an absolute vacuum. Even in intergalactic space there a few atoms in a sufficiently large volume (like a $m^3$). In some respects you can approximate it with an absolute vacuum but in others not (for instance light travels through extremely large distances in intergalactic space, so it will still be affected by many atoms on its way).
How is space a vacuum when there are planets, gases, etc?
Gravity and solar wind scrubs most of it away!
Lucky for us gravity pulls stuff together. This is what made our Sun collapse into a ball and the potential energy released by everything getting closer is what made it hot enough to start nuclear reactions and producing life-giving light.
Gravity also pulled matter together to produce the planets, and big balls of hydrogen (and other things) like Jupiter wouldn't exist either.
The reason our atmosphere's density drops a factor of two every 5-6 km is also due to gravity. Over time, gas seeks out sources of gravity and after a couple of collisions with other molecules forgets where it came from or how much kinetic energy it had. It thermalizes.
But what about those atoms that escape planets?
The tail end of thermal distributions means that some molecules will regularly escape, but the rate is quite slow. Once they do, the solar wind will eventually pick them up and take them out of (at least the inner) solar system.
The solar wind also picks off atoms from planet's atmospheres directly if it can.
This is why Mars' atmosphere is so small now. Without a magnetic field to deflect the charged particles from the Sun (like Earth does) the solar wind ran off with most of Mars' atmosphere. Comments indicate there's recent reconsideration of this.
For more about that I've just asked
So you can run and you can hide, but gravity and/or the solar wind will eventually scour the interplanetary spaces in our solar system and remove most of the gas that would otherwise orbit the Sun.