When a potion is thrown and hits you or a monster, it shatters (releasing vapors) and takes effect, though the effect is not usually as strong as if the potion had been quaffed instead. If you are standing in a square adjacent to where the potion lands, you can still be affected by the vapors relased. For this reason, you should be careful when throwing harmful potions at nearby opponents.
Vapors can also be released if a potion you are carrying gets shattered by something that can vaporize it (such as a fire or lightning). Potions on the ground can be shattered by nearby explosions or blasts from harmful wands. Similarly, bashing open a locked container can shatter potion bottles inside, releasing vapors. A potion can also break if dropped while flying or levitating.
Alchemy accidents will also release potion vapors.
Vapors can be inhaled ("You smell a peculiar odor"), or they can come in to contact with your eyes ("Your eyes water"). Thus, magical breathing or being polymorphed into a creature that does not breathe will not necessarily save you from vapors unless you also have no eyes.
Monsters are not affected by vapors, only by potions that directly hit them.