It has roots in the ancient Celtic festival of Samhain and the Christian holiday All Saints' Day.
The Celts believed that on the night before the new year, the boundary between the worlds of the living and the dead became blurred.
They would light bonfires and wear costumes to ward off ghosts.
The Christian church later incorporated the holiday into All Saints' Day, also known as All Hallows' Day.
The night before became known as All Hallows' Eve, which eventually became Halloween.
The tradition of trick-or-treating likely originated from the medieval practice of souling, where poor people would go door-to-door on All Souls' Day asking for food in exchange for prayers for the dead.
The phrase 'trick or treat' was first used in North America in the 1920s and 1930s, and became popular in the 1950s.