These forms of behavior all take place thanks to communication between individuals.
To communicate, many creatures use signals of one kind or another. The ones used by insects are known as pheromones¸ chemical substances used among members of the same species. They are generally produced in special glands and emitted into the surrounding area, causing changes in insect behavior.
The word itself means "hormone bearers." Indeed, pheromones were once regarded as the equivalent of hormones. Like hormones, they are emitted in small quantities (albeit outside the body) and are responsible for performing a vital function. Pheromones are generally unique to a particular species. There are also some that perform very different functions and in different combinations. Pheromones have a high level of dispersal, and can have an effect from a distance as much as 7-8 km (4-5 miles), with such factors as distance, heat, wind and humidity reducing or increasing their effects.
Pheromones are used for such purposes as sign-leaving, sounding an alarm, gathering members together, for the raising of queens among communal insects, or to control the development of sexual maturity. There are also sex pheromones that work by means of scent.
When reading about animals that communicate through pheromones, one very important point needs to be kept in mind: Every species has its own individual formula, and the chemical substances each contains are all different. The creature that emits the "communicating" substance and the one that receives the "message" are both aware of this formula. Moreover, some creatures also decipher and imitate the formulas belonging to other species.
Left: Smell whatever perfume you use, then deduce what substances it contains. Then try and produce every one of these substances and make a perfume with the exact same smell, of the exact same quality. Unless you have special training in this area, or receive help from experts in the field, you'll of course be unable to do so. The insects that use pheromones to communicate may manufacture them, because God inspires them to do so.
Right: Rhagoletis cerasi