Ganesha also known as Ganapathi is always the first god worshiped before undertaking any work. He is also the remover of all impediments. There are at least two slightly different versions of how he got the elephant head.
According to Shiv Purana, Lord Shiva had gone on war with his ganas. Goddess Parvati, his wife, wanted to take a bath. Having no attendants to guard the door and prevent some one from coming in accidentally, she created a small boy out of the turmeric paste and dirt of her skin. She then infused life into the statue and very beautiful boy was created. She asked him to guard the entrance and not allow any one inside until she said it was okay to do so.
It so happened that Shiva came back from his trip and demanded to enter his own house. The little boy did not know him and refused to disobey his mother. Shiva tried various ways with the boy but the boy refused to let him in. Shiva then sent his ganas to fight the little boy, but they were no match to the little boy who easily defeated them Finally, Shiva went to fight with him and after a fierce battle he severed the head of the little boy.
Hearing the commotion Parvati came out and found her son decapitated. She was filled with sorrow and rage. She demanded the boy be brought back to life. Shiva, who is the destroyer in the trinity Brahma, Vishnu and Shiva, could not bring back the little boy to life. Parvati created numerous shaktis (forms of herself) to devour the ganas. Brahma, the creator and Vishnu, the preserver, came to calm Parvati. Brahma agreed to bring the boy to life but could not use the severed head. Shiva asked his ganas to go and bring the head of an animal who was lying with his head to the north direction. The ganas found an elephant and brought back its head. Brahma put the head of the elephant and brought the little boy to life.
Parvati was now worried for her strange looking son. So, Shiva made him the chief of his gana hence the name Ganapathi(leader of the ganas) or Ganesh (lord of the ganas). Brahma and Vishnu blessed the boy Ganesha that he will always be the first god worshiped and will be the remover of impediments and hence always very popular.
Another version of this story goes like this. Shiva and Parvati were blessed with a baby boy. All the gods came to see the beautiful baby. However, Shani (Saturn) would not look at him. This did not go unnoticed by Parvati. She demanded that Shani look at the baby. Shani looked at the baby and the head burned instantaneously to ashes. Parvati was beside herself in grief. To pacify Parvati, Shiva asked his ganas to get the head of anyone lying with their head to the north. The ganas found a elephant and brought his head back. Shiva revived the baby with the elephant head.
Prayer to Lord Ganesa before starting any work
Vakratunda mahakaya soorya koti samaprabha
nirvighnam kurumay deva sarva karyeshu sarvada.
Vinayaka namasthubyam sarva deva namaskrutha
Avignam kurumey deva sarva karyeshu sarvadha
Sumukascha eka dhanthascha kapilo gajakarnakaha
Lambodharascha vikato vignarajo Gajanana:
Vakra thunda sorupa karnaha heramba skanda poorvajaha
Shodasaithani namani yah pateth srunayadhapi
Vidyarambe vivahacha pravesey nirgamamey thatha
Sangramey sarva karyeshu vignasthasya na jayathey.