Some trace the origins of present day "trick-or-treat" to Samhain, which was the supreme night of demonic jubilation. Spirits of the dead would rise out of their graves and wander the countryside, trying to return to the homes where they formerly lived. Frightened villagers tried to appease these wandering spirits by offering them gifts of fruit and nuts. They began the tradition of placing plates of the finest food and bits of treats that the household had to offer on their doorsteps, as gifts, to appease the hunger of the ghostly wanderers. If not placated, villagers feared that the spirits would kill their flocks or destroy their property.
The problem was... if the souls of dead loved ones could return that night, so could anything else,human or not, nice or not-so-nice. The only thing the superstitious people knew to do to protect themselves on such an occasion was to masquerade as one of the demonic hoard, and hopefully blend in unnoticed among them. Wearing masks and other disguises and blackening the face with soot were originally ways of hiding oneself from the spirits of the dead who might be roaming around. This is the origin of Halloween masquerading as devils, imps, ogres, and other demonic creatures.
Others trace "trick-or-treat" to a European custom called "souling". Beggars would go from village to village begging for "soul cakes" made out of square pieces of bread with currants. The more soul cakes the beggars would receive, the more prayers they would promise to say on behalf of the dead relatives of the donors. At the time, it was believed that the dead remained in limbo for a time after death, and that prayer, even by strangers could guarantee a soul's passage to heaven.
In many parts of Britain and Ireland this night used to be known as 'Mischief Night', which meant that people were free to go around the village playing pranks and getting up to any kind of mischief without fear of being punished. Many of the different customs were taken to the United States by Irish and Scottish immigrants in the nineteenth century, and they developed into 'trick or treat'.