If you have ever watched a beekeeper harvest honey, you've probably seen them use a smoker as they approached the hive. Perhaps you've wondered what this was for?
At a high level the smoke helps the beekeeper to create an atmosphere of harmony with the bees in their hive.
Naturally when a bee detects smoke near the hive (the bees home), it senses the danger that fire might be near by. So they head inside the hive and start preparing to leave home just incase the fire moves closer.
In preparing to leave the hive the bees begin consuming honey and nectar so they will have enough to start up a new hive if they have to move. This change in the bees focus acts as a distraction to allow the beekeeper to check the hives without alarming the entire colony.
The smoke doesn't guarantee that a beekeeper won't get stung but it decreases the likelihood by distracting the bees and decreasing the amount of "alarm pheromone" the bees release.
When a bee releases the alarm pheromone other bees in the colony smell it and get upset and can start attacking.
In fact often if you are stung by a bee you will then be repeatedly stung in the same place due to the bees smelling the alarm pheromone and reacting.
Therefore if you do get stung, it is a good idea to wash your clothing before approaching the hive again, as bees will smell the pheromone left on your clothing during future visits and potentially attack.