TL;DR: The Star Wars galaxy possesses magnetic shield technology, in which a molecule-thin magnetic field prevents air from passing through it, yet somehow allows larger mass through.
The technology used to protect hangars from open space advances somewhat during the Star Wars saga, so I'll discuss both types of technology that we see on-screen.
Galactic Empire: A dynamic magnetic shield
In the original trilogy, hangar bays (as well as any other areas open to space) are sealed using a magnetic shield. The magnetic shield is an electromagnetic field that prevents gases from passing through it, but not solid material. In most cases, a vessel can pass through the shield without requiring deactivation. This allows hangar operations to continue without interruption.
However, in some cases, such as when a ship has a polarized hull which would interfere with normal shield operation, the hangar must be cleared and the shield deactivated before the vessel can enter the hangar. I don't have it handy at the moment, but this was specifically discussed in one of the EU novels.
The shield itself cannot be seen, so areas protected by a magnetic shield have lights set around the opening to indicate shield activation. On Imperial bases and ships, a bright white light is used to show that the shield is on. Some other versions of the technology use a blue light instead.
Old Republic: A static force field
Note that earlier versions of the shield were an actual shimmering "force field". This earlier version did not require any sort of visual indicator since the shield itself could be plainly seen. The older style of shield, however, had to be disabled before a ship could pass into or out of the hangar bay. This is clearly seen during the Battle of Coruscant in Episode III, during which Anakin must destroy the field generator before he and Obi-Wan can fly into the ship's hangar and begin their rescue of Palpatine.
Non-Aligned: A dynamic magnetic shield
In the Legends novel "Planet of Twilight", Leia finds herself secured in a mansion on the planet Nam Chorios. A desolate world, the owner of the mansion - one of the wealthiest men on the planet - apparently uses magnetic shielding to maintain a more humid environment indoors... despite having open windows that let in sunlight. In the passage below, Leia notices this as she walks in from a balcony:
The air was softer indoors, subtly modified to escape the piercing
dryness. That meant magnetic shields on the doors and windows - not
cheap - and some kind of mist generators in the ceilings.
Since she had just walked through the open doorway from an outside balcony, this seems to prove that magnetic shield technology allows objects of sufficient mass to pass through them, yet somehow prevents air or water molecules from passing through the same opening.
Furthermore, Nam Chorios was considered a backwater world. Nobody on the planet - including the owner of the mansion - had access to modern technology. This would indicate that the magnetic shield technology had been around for quite some time (despite still being expensive).
The technology is mentioned again in the Legends novel "Legacy of the Force: Exile" as Lando watches the Falcon land in a hangar bay:
He watched as the Falcon slowly approached. As frail as she looked, he
half expected her to bounce off the atmospheric shields that kept the
vacuum of space at bay, but she floated gently in through that
The novel takes place approx. 40 years after Episode VI, so this idea of a magnetic field barrier that keeps in the atmosphere remains consistent for nearly a century across both movies and novels.