The Christian Church co-opted the feast of Samhain, the beginning of the Celtic New year for the feast of All Saints or All Hallows (Holy) Day. In the Celtic traditions, the evening before the first day of the year was a time for many spiritual creatures to manifest themselves on earth, including the return of the spirits of the dead to there earthly habitats.
A great many of the celebratory rites of the pagan feast, intended to either placate or ward off any evil spirits, carried on after the feast was “baptized” (hence jack-o-lanterns, noise making, and the association of scary things).
A tradition of pranks and practical jokes arose in Ireland. (Imitating the wandering spirits? Simple mayhem on a rowdy night?)
The trick-or-treat tradition appears to have begun in the murky traditions of the Irish immigrants to the U.S. (although there are several conflicting versions that link it back to various pagan rituals) with one version being that young hooligans would coerce treats from homeowners by threatening to perform their (nasty) tricks if they were not bribed with treats.