Bees belong to an ancient species that has continually adapted to the many challenges posed by the environment. As a result, a highly organized society has evolved. The development of a community lifestyle to ensure survival is but one example of the species' evolutionary adaptations.
The hive of the honeybee provides a delectable prize for many predators. Insects such as ants, wasps, and other bees are common intruders, as well as many mammals - bears, skunks, badgers, raccoons, possums, anteaters, mice, and humans. The attack behavior of bees developed as a defense to certain stimuli that signal the hive is in danger from an intruder. When honeybees attack in large, they are defending their colony. A bee will rarely sting when it is away from the colony foraging on pollen, nectar or water. However, a bee may sting if it is handled roughly (swatted at or stepped on), or feels alarmed in any way. Generally, if you leave a bee alone, it will leave you alone.
The following is an excerpt about colony defense from Bees and Beekeeping - Science, Practice, and World Resources by Eva Crane.
The basic unit of colony defense is an individual worker whose venom sac contains venom and is thus able to sting. If stinging occurs, it is usually the last of the bee's responses to certain stimuli that culminate in an attack, as described below.
Venom is produced in the worker's venom gland and stored in the venom sac, which is likely to be filled by the time the worker is 14 days old. The age distribution of the bees in a colony is thus relevant, and colonies with many bees less than 2 weeks, whose venom sacs are not yet filled, show relatively little defensive behavior. A worker that stings another bee can usually withdraw her sting without injury to herself, but if she stings into thick skin (as of a person or animal) she usually dies, being unable to retract her sting.
The figure below sets out the time sequence of the behavior of a bee in defense of her colony.
The bee's response to the first (alerting) stimulus strengthens her guarding stance; for instance the abdomen is raised, possibly with the sting protruded, and the antennae are waved. In addition, the bee may recruit other bees to guard activity, by entering the colony with her sting chamber open and the sting prodtruded, thus releasing alarm pheromone. The second (activating) stimulus causes the bee to search for the source of disturbance. When she locates it, the third (attracting) stimulus makers her orient herself towards it and move there. As a result of the fourth (culminating) stimulus, she attacks the target: she threatens it, emitting a high-pitched buzz and making body thrusts towards it. The attack itself may consist of biting, burrowing into hair, pulling hairs, and stinging, which - if she cannot retract her sting - is her final act in defense of the colony. At stage 1, unresponsive bees may move away from the source of disturbance, and at stage 4, many are likely to do so, running to undisturbed combs (which the beekeeper tries to make them do, by the use of smoke), or flying away from the colony.
The behavior of different colonies varies greatly, according to the colony's general make-up, and to certain environmental and colony conditions. Environmental factors include: temperature, and variation between day and night temperatures; light intensity and rate of change of day length; atmospheric humidity, pressure and electric potential; magnetic field. Food availability is important, and its diminution (as at the end of flow) usually increases defensive behavior, as does robbing by bees of other colonies, which is likely to occur when forage has become scarce. Lack of water does so, probably also contamination of bees with some insecticides, and apparently foraging on certain nectars and pollens.
Overcrowding of bees in a hive, or of hives in relation to forage available, is likely to increase defensive behavior. On the other hand colonies weakened by disease, by a failing queen or by lack of food, cannot mount an effective defense; this situation is exploited by predators, and by honeybees from stronger colonies - especially after the last flow of the season has ended and many would-be foragers are searching for any food that they might collect.
Heritable factors are very important in determining defensive behavior, and give rise to differences between races and also within the same race or strain.