Bees are pretty sensitive to movements, so if you see some moseying around, then try to move slowly and smoothly and they should ignore you. Swatting at them or waving them away will likely send them into Alarm mode.
Bees won't usually sting you out of pure spite. The chances are that you accidentally hurt it or gave it cause for concern without even knowing. This could have been stepping on it, or squishing it in a fold in your clothing, or just batting it away.
Once bees release the "Alarm" pheromone, all you can really do is vacate the immediate area.
Alarm
The complexity of pheromones in bees is illustrated well by the two types of alarm pheromone, which can be distinguished by which glands release the pheromone.
- From the Koschevnikov gland: This gland is near the sting shaft and is released when a bee stings. The release of the alarm pheromone is a defensive reaction to alert nearby bees. This alarm pheromone smells like bananas. If you are unfortunate enough to be stung, you may wish to leave the area as you tend to the sting, because alarm pheromones are being received by other bees.
- From the Mandibular glands: This consists of 2-heptanone and is used as an anaesthetic and to paralyze intruders, after which bees remove the intruder from the hive.
Note here the pheromone that's most likely to relate to you smells like bananas, so if you're eating one, it might be a good idea to drop/dispose it.