Halloween has its roots in the Celtic people and their festival of Samhain (which is pronounced SAU-an). It was a magical time, according to the author David Skal.
It was also a time of demons, as a few lines from an ancient tale demonstrate:
Great was the darkness of that night and its horror,
and demons would appear on that night always.
(From The Adventures of Nera, an ancient Celtic tale about Samhain; see page 4 of the PDF file for an English translation.)
Modern writers shed more light on the meaning of the ancient festival:
The festival of Samhain was the most sacred of all Celtic festivals. Its rituals helped link people with their ancestors and the past. The Celts believed that the dead rose on the eve of Samhain and that ancestral ghosts and demons were set free to roam the earth.
Since spirits were believed to know the secrets of the afterlife and the future, the priests of the Celts, the Druids, held that on the eve of Samhain predictions had more power and omens could be read with more clarity. They divined the health of the tribe, the wisdom of a proposed move, the right time to make magic or the key to curing a sickness. (Halloween, by Lesley Pratt Bannatyne, page 2.)
The festival occurred at the time of year when nights, in Northern Europe, were getting long:
Samhain marked the start of the season that rightly belonged to spirits - a time when nights were long and dark fell early. It was a frightening time for a people who were entirely subject to the forces of nature, and who were superstitious about the unknown, with only a primitive sympathetic magic system to rely on for comfort. Shamhain was a night of mystical glory. (Bannatyne, pages 2 and 4.)
What would the "forces of magic" accomplish on Samhain eve?
. . . the night of its eve was the great occasion in the year when the temporal world was thought to be overrun by the forces of magic. Magic troops issued from caves and mounds, individual men might even be received into these realms; whilst against the royal strongholds, assaults by flame and poison were attempted by monsters. (Bannatyne, quoting at page 4.)
Gathering around a huge bonfire, on Samhain, Celtic villagers would send the spirits of the dead back to the "spirit world" - an effort to keep them away from living people.
Then ... in the 7th century - as the influence of the Catholic Church grew - the Celtic holiday of Samhain was merged with a religious holiday known as "All Saint's Day" (during which people honored the memory of dead saints and martyrs). "All Saints Day" was celebrated on November 1st.
The original name for "All Saints Day" was Hallowmas - meaning "holy" or "saintly" (for "Hallow") and "mass" (for "mas"). Hallowmas, roughly translated, therefore means "Mass of the Saints." The night before Hallowmas - October 31st - was known as "All Hallows' Eve" (which gradually became "Halloween").
In the mid-nineteenth century, when the Irish Potato Famine devastated Ireland, millions of starving people fled their country. Some of those Irish immigrants - along with their cultural traditions - came to America. One of their traditions included playing pranks on Halloween (with children wearing masks so they would not be recognized).
As the years passed, childish pranks and tricks turned into serious acts of vandalism. By the 1930s, young hooligans were demanding the payment of candy ("treats") in exchange for not damaging people's property ("tricks"). One could say, therefore, that collecting candy on Halloween began as a "form of extortion."
By the late 1930s, as children went door-to-door in their neighborhoods, "trick or treat" became the Halloween greeting. And ... so it remains to this day.