Technically we should not be referring to the child as Ganesha at this stage (Ganesha = Gana + isha, i.e Lord of the ganas; a title he gets later), but nevertheless since the boy created by Parvathi was nameless, let us use Ganesha. There are several versions of how and why Shiva beheaded Ganesha. The most popular seems to the version where Ganesha is created by Parvati, and obstructs Shiva from seeing his wife. Let us take this up for discussion:
1) The mythological level: Ganesha when being created by Parvati was not told who his father is, he was not told who he himself was. He was simply asked to stand guard. It was a standing instruction. And that’s exactly what he did. Now Shiva comes along and is surprisingly denied right on entry in his own house! Shiva, the Lord of Destruction naturally gets annoyed and beheads Ganesha. Then the whole process of appeasing Parvati by affixing an elephant’s head onto the boy’s head ensues.
2) The spiritual level: Now one can argue that Shiva is omniscient and omnipresent. How can HE not know who Ganesha is? The reason ascribed here is that of “Leela”, the God’s desire to sport, to create Maya, mithya and then dispel it for “Loka-kalyana”. Here, the leela is construed as a means of providing the world “a Lord of Obstacles” – “Vighna (obstacles) + Ishwara (God)”, i.e Vighneshwara. That is the boon Vinayaka gets when Parvathi fears her son with his elephant head will be made fun of. To this day, all auspicious events in the Hindu liturgical fashion begin with an invocation of and appeasement to this Vighnaraja, Ganesha. As a form of Shiva – tattva, Vighneshwara is the destroyer of obstacles.