The term trick or treat is closely associated with the holiday of Halloween, which has a name worthy of an article of its own. Go there if you want, but this article here is where the sweet stuff is.
The Origins of Halloween and Trick-or-Treating
The true origins of the whole "dressing up in a costume and going around to various houses to greet anyone who answers your knock with a friendly threat" thing is unknown. But according to Encyclopedia Britannica (hi, mom) the traditions of Halloween have their origins in Samhain, a festival celebrated by the Celts of ancient Britain and Ireland. Samhain marked the end of summer and the onset of winter, and occurred on a date that corresponds to our November 1st. It was believed that during the Samhain festival, the world of the gods was visible to humans, and the gods took advantage of this fact by playing tricks on their mortal worshippers. Those worshippers in turn responded with bonfires on hilltops and sometimes masks and other varied disguises to keep ghosts from being able to recognize them. Things tended to get spooky and dangerous around Samhain, with bloody sacrifices and supernatural phenomena abounding. Not quite the neighborhood haunted house, but maybe there's the faintest of echoes there.
Samhain chugged along for centuries, until Christianity poked its nose in: in the 8th century CE, All Saints' Day, a somewhat new Christian holiday, got moved from May 13th to November 1st. And the evening before All Saints' Day became a holy—that is, a hallowed—eve. Within a few centuries—my, how time flies—Samhain and the eve of All Saints' Day had been merged into a single holiday.
Protestants of the Reformation and all that came after largely rejected the whole thing, but the holiday persisted among some communities. 19th-century immigrants to the U.S., including many from Ireland, brought their Halloween customs with them and deserve no small amount of credit for the holiday as it's celebrated in the U.S. today.
Earliest Uses of 'Trick or Treat'
Still, the trick-or-treating took some time to settle in. The authority on how that term first settled into the lexicon is Barry Popik, an independent etymologist who's also uncovered the mysteries behind such culturally important terms as hot dog and Big Apple. It is he who has found the earliest known examples of trick or treat in use.
Popik's research traces early iterations of the term to the northwestern part of North America, specifically the Canadian provinces of Alberta and Saskatchewan:
Hallowe’en passed off very quietly here. 'Treats' not 'tricks' were the order of the evening.
— The Leader-Post (Regina, Saskatchewan), 2 Nov. 1923Hallowe'en night was observed in the usual manner by the young "bloods" in Penhold. "Fun is fun, and tricks are tricks," but when such public buildings as school and Memorial Hall are molested with no option for "Treat or Trick," we can not see where either fun or trick is enjoyed by the participants.
— Red Deer (Alberta) Advocate, 7 Nov. 1924
A few years later, the phrase is used in a more familiar way:
Hallowe’en came and went and was observed most circumspectly in town, without the usual depredations. The greatest activity was manifested by the very young, who wandered in droves from door to door, heavily disguised and demanding “trick or treat.” To treat was to be untricked, and the youthful hold-up men soon returned home bowed down with treats.
— T. D. Colcord, Calgary (Alberta) Daily Herald, 3 Nov. 1927Hallowe’en provided an opportunity for real strenuous fun. No real damage was done except to the temper of some who had to hunt for wagon wheels, gates, wagons, barrels, etc., much of which decorated the front street. The youthful tormentors were at back door and front demanding edible plunder by the word “trick or treat” to which the inmates gladly responded and sent the robbers away rejoicing.
— Lethbridge (Alberta) Herald, 4 Nov. 1927
The first U.S. example Popik reports is from Michigan, and in the plural:
“Trick or Treats?” (headline) … the peaceful citizens lived in terror of the time each evening when they should be summoned to their front doors to hear the fatal ultimatum “Tricks or treats!” uttered in a merciless tone by some small child who clutched in one grubby fist a small chunk of soap capable of eliminating the transparency from any number of windows.
— Bay City (Michigan) Times, 1 Nov. 1928
The earliest example in Merriam-Webster's files is from 12 years after the Michigan example:
The local equivalent here (Decatur, Illinois) is "Trick or treat." The custom is the same: children masked and in costume knock at front doors and greet the host with "Trick or treat!" (in a somewhat disguised voice). The proposal is, obviously, a mild kind of blackmail in which—whatever the motives—the treats are always forthcoming.
— John Valentine, letter in American Notes and Queries, March 1942
The writer above was responding to an article entitled "Halloween: Shell Out," shell out being another phrase employed in the same context.
Later Use
Trick or treat as a set phrase had yet to fully gel as late as 1965, when in a Peanuts cartoon Charles Schulz has Lucy engaged in the activity with the words "TRICKS OR TREATS..." in the speech bubble hovering above her.
And in 1972 a Merriam-Webster editor wrote a note objecting to the defining of trick or treat as "the Halloween practice" rather than "a Halloween practice" because "the beginning word 'the' makes it sound like a universal practice. But I never heard of it when I was a child in southern Illinois. Say 'a' instead?" The definition was so revised.