One day Goddess Parvati was at home on Kailash preparing for bath. As she didn’t want to be disturbed by anyone, she told Nandi, to guard the door and let no one pass by. Nandi faithfully took his post, intending to carry out Mata Parvati’s wishes. But, when Lord Shiva(Mahadev) came home and naturally wanted to come inside, Nandi had to let him pass, being loyal first to Lord Shiva . Parvati was angry at that moment, but even more than this, at the fact that she had no one as loyal to herself as Nandi was to Lord Shiva. So, taking the dirt from her body and using her power gave life into it, she created Vinayaka, declaring him to be her own loyal son as there is no one at kailash.
This is how Lord Ganesh came into existence.The next time Goddess Parvati went for bath, she told Vianayaka that he had to guard at the door. At that time Lord Shiva came home(as he went out for killing the Asuras), He want to meet Goddess Parvati but he was stopped by Vianayaka. Vianayaka told that he can't allowed him to meet his mother(Goddess Parvati) as she ordered him that no one should be allowed to cross the door. Then Lord Shiva told him that he is the Husband of Parvati and Vianayaka was his son.
Vianayaka told that even he was his father he has no right to enter in. Furious, Lord Shiva ordered his army to hold the boy, but they all failed!
Such power did Vianayaka possess, being the son of Devi Herself. Now, Lord Shiva got anger and started 'Shiva Tandav'.But, as a father his controlled his anger and then send Nandi to tell him that he is his and he had to obey him. Nandi went there and got insulted by Vianayaka. Lord Shiva went there and tried to explain Vianayaka what is right and what is wrong. But, Vianayaka tried to attack on Lord Shiva. Now, Lord Shiva was helpless and forcefully he had released his Trishul at Vianayaka (as once he was cursed by the father of God Surya(Sun) when Lord Shiva killed God Surya , that he will kill his own son).
By this Vinayaka head turned into ashes. When Goddess Parvati come to about it, she imediately came out and told Lord Shiva that he had to return life to his son. Lord Shiva said to Parvati that it is impossible as he's head was turned into ashes by his own Trishul, no such thing in this world was created that can be returned which was destroyed by his Trishul. Now, Adishakti(Goddess Parvati ) got angry and turned into 'Kali Matha' and said to Lord Shiva that if his son is life can't be revived then whole universe will be destroyed and she started destroying the universe. Then Lord Vishnu and Lord Brahman got appeared and said to Lord Shiva to stop Adhishakti. Lord Shiva replied to them, that she can be stopped by one way and called Nandi (bull) and told him that move towards
North and ask the first creature who is willing to give his head for Vinayaka and also said he should be returned by evening. Nandi went towards North and saw a old elephant lying there and he went towards him and asked his head. The elephant immediately accepted him. Nandi thanked him and also asked a question why he was accepted him immediately. Then the elephant told him that once he was doing Tapasya for impressing Lord Shiva, Lord Shiva got impressed and appeared and gave him a boon, he said that he want to become the greatest scholar in this universe. But, Lord Shiva himself is the greatest Scholar so, he want to live with him and want to gain knowledge from him then Lord Shiva granted his wish and said him that in future he'll live with him. Now, it's the time to live with Lord Shiva so, with my williness i'm giving my head. Nandi got happy and took his head to Lord Shiva. Lord Shiva kept the elephant head to Vinayaka. By, seeing this Goddess Parvati calm down with his anger. Lord Shiva apologize to Parvati and named his son as 'Ganesh'.
Lord Brahma and Lord Vishnu gave blessing to Lord Ganesh and left from there.