It started with bread.

In the Europe of the Middle Ages and into the Renaissance, bread was made, in large part, with rye. And rye and rye-like plants can host fungus—ergot*—that can, when consumed in high doses, be lethal. In smaller doses, however, ergot can be a powerful hallucinogen. Records from the 14th to the 17th century mention Europeans' affliction with "dancing mania," which found groups of people dancing through streets—often speaking nonsense and foaming at the mouth as they did so—until they collapsed from exhaustion. Those who experienced the "mania" would later describe the wild visions that accompanied it. (In the 20th century, Albert Hofmann would realize the psychedelic effects of LSD while studying ergot.)

A 17th-century wood engraving of a "witch" being prepared for "flight" (Wellcome Institute, London, via John Mann)

So people, as people are wont to do, adapted this knowledge, figuring out ways to tame ergot, essentially, for hallucinatory purposes. And they experimented with other plants, as well. Forbes's David Kroll notes that there are also hallucinogenic chemicals in Atropa belladonna (deadly nightshade), Hyoscyamus niger (henbane), Mandragora officinarum (mandrake), and Datura stramonium (jimsonweed). Writing in the 16th century, the Spanish court physician Andrés de Laguna claimed to have taken "a pot full of a certain green ointment … composed of herbs such as hemlock, nightshade, henbane, and mandrake" from the home of a couple accused of witchcraft.

So why do the brooms fit into this? Because to achieve their hallucinations, these early drug users needed a distribution method that was a little more complicated than simple ingestion. When consumed, those old-school hallucinogens could cause assorted unpleasantnesses—including nausea, vomiting, and skin irritation. What people realized, though, was that absorbing them through the skin could lead to hallucinations that arrived without the unsavory side effects. And the most receptive areas of the body for that absorption were the sweat glands of the armpits ... and the mucus membranes of the genitals.

So people used their developing pharmacological knowledge to produce drug-laden balms—or, yep, "witch's brews." And to distribute those salves with maximum effectiveness, these crafty hallucinators borrowed a technology from the home: a broom. Specifically, the handle of the broom. And then ... you get the idea. 

From M. J. Harner's Hallucinogens and Shamanism, via Alastair McIntosh

According to an investigation into witchcraft from 1324

In rifleing the closet of the ladie, they found a pipe of oyntment, wherewith she greased a staffe, upon which she ambled and galloped through thick and thin. 

And here's Jordanes de Bergamo, writing in the 15th century

The vulgar believe, and the witches confess, that on certain days or nights they anoint a staff and ride on it to the appointed place or anoint themselves under the arms and in other hairy places.

So that explains the brooms. And what about the flying?

Part of the connection may have to do with brooms' place in pagan rituals. As a tool, the broom is seen to balance both "masculine energies (the phallic handle) and female energies (the bristles)"—which explains why it was often used, symbolically, in marriage ceremonies. But the more likely connection has to do with the fact that users of "witch's brew" were, in a very practical sense, using their ointment-laden broomsticks to get high. They were using their brooms, basically, to "fly." 

Indeed. Here's how Gustav Schenk described the effects of tropane alkaloid intoxication, in 1966: 

My teeth were clenched, and a dizzied rage took possession of me … but I also know that I was permeated by a peculiar sense of well-being connected with the crazy sensation that my feet were growing lighter, expanding and breaking loose from my own body. Each part of my body seemed to be going off on its own, and I was seized with the fear that I was falling apart. At the same time I experienced an intoxicating sensation of flying …. I soared where my hallucinations—the clouds, the lowering sky, herds of beasts, falling leaves … billowing streamers of steam and rivers of molten metal—were swirling along.

So there you have it, rye to flying brooms. But "witches" in the cultural imagination, of course, don't necessarily need re-purposed cleaning supplies to be accused of sorcery. In 1976, Linnda Caporael presented work suggesting that the Massachusetts of the late 17th century had been the unknowing victim of an outbreak of rye ergot. Her work is the subject of continued debate, but has been substantiated by later scholars: The Massachusetts of 1692 likely did see an outbreak of the fungus that had contributed, in other contexts, to "witch's brew." 

The epicenter of the outbreak? Salem. 

This section updated to remove references to ergot forming on already-baked bread; ergotism results from the grain itself being tainted.