The origin of Ganesh Chaturthi festival lies in the Holy Hindu scriptures which tell the story of Lord Ganesha. Lord Ganesha (or Ganapati) (the names mean "Lord [isha] or [pati] of Shiva's hosts [gana]") was created by Goddess Parvati, consort of Lord Shiva.
According to the legend, Lord Shiva, the Hindu God of resolution, was away at a war. His wife Parvati, wanted to bathe and having no-one to guard the door to her house, conceived of the idea of creating a son who could guard her. Parvati created Ganesha out of the sandalwood paste that she used for her bath and breathed life into the figure. She then set him to stand guard at her door and instructed him not to let anyone enter.
In the meantime, Lord Shiva returned from the battle but as Ganesha did not know him, stopped Shiva from entering Parvati's chamber. Shiva, enraged by Ganesh's impudence, drew his trident and cut off Ganesha's head. Parvati emerged to find Ganesha decapitated and flew into a rage. She took on the form of the Goddess Kali and threatened destruction to the three worlds of Heaven, Earth and the subterranean earth.
Parvati was still in a dangerous mood. Seeing her in this mood, the other Gods were afraid and Shiva, in an attempt to pacify Parvati, sent out his ganas, or hordes, to find a child whose mother is facing another direction in negligence, cut off his head and bring it quickly. The first living thing they came across was an elephant. That elephant was facing north (the auspicious direction associated with wisdom). So they brought the head of this elephant and Shiva placed it on the trunk of Parvati's son and breathed life into him. Parvati was overjoyed and embraced her son, the elephant-headed boy whom Shiva named Ganesha, the lord of his ganas. Parvati was still upset so Lord Shiva announced that everyone who worships Ganesha before any other form of God is favoured. So Ganesh is worshipped first in all Hindu occasions and festivals.