OBVIOUS SIMILARITIES suggest at least a notional link between the present-day Halloween custom of wearing costumes and trick-or-treating on October 31 and the Medieval practices of "mumming" and "going a-souling" on the eves of All Saints Day (November 1) and All Souls Day (November 2). Mumming took the form of wearing costumes, chanting, singing, play-acting, and general mischief making, while souling entailed going door to door and offering prayers for the dead in exchange for treats, particularly "soul cakes."
Another likely antecedent was the British custom, dating from the 1600s, of youths wearing masks and carrying effigies (including jack-o'lanterns carved from turnips) while begging for pennies on Bonfire Night (also known as Guy Fawkes Night), the November 5 commemoration of the so-called Gunpowder Plot to blow up Parliament in 1605. (While not an official holiday, Bonfire Night is still celebrated in parts of England.)
By the mid-1800s when Irish immigrants brought their version of Halloween to North America, however, the customs of mumming and souling were all but forgotten in Ireland and England (though a variant of mumming known as "guising" survived in Scotland); Americans, for the most part, had no idea who Guy Fawkes was, let alone why anyone should go begging for "pennies for the Guy;" and, despite Halloween becoming permanently ensconced as an American holiday by the turn of the 20th century and the festivities associated with it often included the wearing of costumes, there's no mention in published sources of "trick-or-treating" or anything resembling it before the 1930s.
One does find mention — many mentions, in fact — of unrestrained pranksterism and vandalism on Halloween night dating from the late 1800s on, thus one current theory holds that trick-or-treating was an early-20th-century contrivance meant to provide an orderly alternative to juvenile mischief (essentially bribing the would-be tricksters with treats).
Following Anglo-Irish tradition, Halloween parties featuring fortune-telling games (such as bobbing for apples) and other supernatural trappings were common practice by the turn of the 20th century, and these morphed into costume parties with children dressing as witches, ghosts, and goblins. Perhaps the simplest explanation for the emergence of trick-or-treating is that someone had the inspiration to take the costume party door-to-door.
Whatever the precise details of its origin (which we may never know), by the 1940s trick-or-treating had become a Halloween fixture throughout the United States, and remains so to this day.
• History of All Saints Day - ChurchYear.net
• History of All Souls Day - Catholic Encyclopedia
• Guy Fawkes or Bonfire Night - About.com: U.K. Travel
• Origin of 'Trick-or-Treat' - The Phrase Finder
• How Halloween and Candy Became Best Friends - The Atlantic
• Halloween: The Fantasy & Folklore of All Hallows by Jack Santino
• History & Origin of Halloween - About.com: Urban Legends