I was told a story with no documentation that it was based on the idea of the harvest, and the chidren of poor families went to the farmers houses dressed to conceal their identities to receive part of the different farmers harvest. Early time welfare??? Who knows.
That was the origin of the trick or treat part itself. Kind of like a big potluck, I guess.