The word "Halloween" is derived from a few things; All Saints Day (1st November), Eve of All Hallows (All Hallows Eve)or Hallow Even.
The celebration of Halloween was originally the New Years Eve of the Celts, the eve of Samhain or "Winter's Eve". It was generally believed that the dead or disembodied spirits ofall those who had died throughout the preceding year would come back in search of living bodies to possess for the next year. It was believed to be their only hope for the afterlife. All who werealive of course didn't want to be possessed by these lost souls so on the night of October 31st, villagers would extinguish the fires in their homes to make them cold and inhospitable then dress up inghoulish costumes and parade around the neighborhood making as much noise as possible to frighten away the spirits looking for a live body to possess. Food and drink (which I'd say accounted for thecrazy actions of the villages, not the fear of being possessed) would be left out for these costume wielding villages.
It was believed in ninth-century Europe that after you died, your soul remained inlimbo. To send the souls up to Heaven the Christians used to go "souling". On 31st October, All Souls Day, they would go around to all the villages and collect "soul cakes" - a cake made out of squarepieces of bread with currants. The relatives of the dead would pass the cakes on to the Christians who would promise to pray for their relative which in turn would send them on to heaven. The morecakes given - the more prayers said. This is one custom which was thought that trick-or-treating originated from.
It is also thought that trick-or-treating came from Britain where it was know as"Mischief Night", it is particularly popular in parts of northern England.
It could also of come from old Irish peasant practice in which they would go from door to door to collect money, breadcake,...