But just why are whales so intelligent? The answer may lie in the evolutionary history of an invertebrate lineage totally unrelated to cetaceans, but that features similar evolutionary pressures. I'm talking, of course, about the class Cephalopoda.
Thanks to the various evolutionary pressures involved with their aquatic environment, whales have evolved a series of unique neurological adaptations in addition to their remarkable intelligence. Energy-conserving thermoregulatory processes in the brain and differential release of neurotransmitters like acetylcholine allows whale to sleep with one half of their brain in deep, non-REM sleep while the other half remains alert, a process called asymmetric slow-wave sleep (ASWS evolved independently in pinnipeds -- seals, sea lions). Whales do not need a gag reflex, as the whale trachea does not open to the esophagus. Breathing is a voluntary and conscious process for whales; consequently, anesthetizing a whale could kill them.
While all cetaceans have larynxes, the process, their process of phonation is very different from our own. Odontocetes (toothed whales, like sperm whales, dolphins and porpoises) produce sounds by passing air through their nasal passages, a complex series of air sacs and plugs that control the passage of air between them. Until 1961, most scientists thought that phonation in odontocetes was laryngeal, the most phylogenetically parsimonious assumption given that most all mammals vocalized laryngeally. The odontocete larynx is actually powerfully muscled, with their elongated, spout-like epiglottis (the flap that, in humans, closes over the larynx when swallowing occurs) secured by a sphincter-like complex of striated muscles called palatopharyngeal muscles. However, during the 1960s scientists realized that the vocal cords did not vibrate during phonation, and through the use of a hydrophone realized that odontocete sounds came out of the forehead or melon. Eventually scientists mapped the nasal passages of the sperm whale and discovered the dorsal bursa (or "phonic lips"), flaps of tissue that vibrate and produce toothed whales' high-frequency clicks, buzzes, and whistles – similar to the vocal cords in our larynxes. The melon, a fatty mass within the odontocetes' foreheads, plays an important role in shaping and directing sounds used in echolocation. Air that has passed through the phonic lips can pass back around the sinuses or be exhaled. With the exception of the Sperm Whale (Physeter macrocephalus), all species of odontocetes have two pairs of dorsal bursa, meaning that they can produce two different sounds simultaneously.
Baleen whales phonate in a fashion much more similar to humans, in that they produce sounds by passing air through their vocal cords. However, they do not exhale as part of this process, instead recycling the air that passes through their larynx. Humpbacks can sing for 15-20 minutes without breathing in or out. The details of how baleen whales manage this are still poorly understood.
There are two broad categories into which whale vocalization can be grouped: the pulsed calls, whistles and other social sounds that whales produce for the purpose of communication with conspecifics, and clicks that allow odontocetes to track down prey. Whistles, squeaks, screams and squawks serve a a myriad of social purposes within odontocete pods, including the coordination of hunting strategies, mother-child recognition, and play. Baleen whales' songs are a sexually selected characteristic (they also use grunts, groans, snorts and barks to communicate). Many whales also slap their pectoral or tail fins against the surface of the water to produce sounds, a behavior called lob tailing.
The most prolific and well-documented singer of the Mysticete suborder is the Humpback Whale (Megaptera novaeanglia). Humpbacks sing most often during mating season, but have also been heard singing during migration and on their Arctic feeding grounds at a "level of organization and structure approaching that of typical breeding ground song". Humpbacks also sing during active diving, at depths of up to 100 meters. Humpback songs consist of 5-7 themes, typically varying widely in frequency, that are repeated sequentially. A song may last anywhere from five minutes to half an hour.
Only males sing, and thus Humpbacks' songs were originally thought to be a means of attracting the attention of females. Females have not been observed to directly respond to songs, however, and so this hypothesis has been largely rejected. Scientists have proposed that Humpback song offer a means of communication between males (who do respond to others' song). They've also suggested that songs act as a migratory beacon, or that they synchronize estrous cycles in females.
We don't really know that the calls that whales produce are languages in the strictest sense of the word, but they seem to have immense social significance. A recent study of 200 Common Bottlenose Dolphins (Turiops truncatus) off the coast of Scotland found that Bottlenoses have their own signature whistle, which they develop in their first few months of life and continue to react and respond to.