As Lord Shiva is himself one of the Trimurti, he must be a त्रिकालज्ञ (trikālajña) i.e., knower of past, present and future.
If he's a trikālajña, why didn't he know that he was going to behead his own son, Vināyaka (Ganesha)?
This whole event is a series of interconnected Chain events. I'm here summarizing the chain events:
Gajasura was a devotee of Lord Shiva and he got a boon that his head would be revered in future as described in the answer here.
Lord Shiva once killed Surya and Kasyapa (father of Surya) cursed him as described in answer here.
He struck Surya with a trident and this made the entire world plunge into darkness. Surya was the sage Kashyapa's son and so, for this act, Kashyapa cursed Shiva. "Just as you pierced my son's chest with a trident, your son's head will be chopped off one day," said Kashyapa.
Ganesha (Vinayaka) was only son of Prakriti tatwa as made only by Parvati. Lord Shiva transformed Purush tatwa to Vinayaka from his Trishula while beheading him. And thus he philosophically became son of both Shiva and Shakti.
Ganesha was formed from dirt/mud by Parvati. So his intellect had also became dirt formed intellect. As a result he couldn't recognise the Supreme Shiva and other Gods, Sages also. So, Beheading the head removed the dirt formed intellect and it also removed only Maya/Prakirti/Parvati formed intellect.
These all events described above are interconnected as Chain events. So, Beheading Vinayaka (Ganesha) by Lord Shiva was necessary.
I would like to quote the following text hope this helps
The story goes that Parvati created a boy out of dirt from her body and appointed him to guard the doorstep. The first question that comes is how could Parvati, the Goddess Herself, have so much dirt? Parvati symbolizes the triguna sattva, rajas and tamas. The entire creation is made up of trigunas. The dosha or imperfections that originate out of the triguna is an obstruction for the functioning of Prakriti. That was the boy that Parvati created to stand guard at the door.Shiva is Shuddha Chaitanya, pure consciousness. Like the sun does not recognize darkness and cuts through it, Shiva does not recognize dosha and slays the impurity. But Prakriti cannot stay without impurity. So Shiva replaces the dosha with the head of an elephant, which symbolizes knowledge. Through knowledge, all the doshas or obstructions of Prakriti can be taken care of. This is the spiritual and metaphysical meaning of Ganpati being slain. So worshipping Ganpati as the remover of obstacles and the giver of gyana, knowledge is the most amazing depiction of the nature of consciousness.
For more info refer here
Because lord Ganesha was created with mud his buddhi(intellect) was dushit(contaminated) he didn't respected other gods & rishis who came and tried to tell him the truth that Lord Shiva was his father. So that's the reason Shiva cut his head and replaced it with the one of an elephant which is a symbol of knowledge. The elephant whose head was cut and replaced with that of Lord Ganesha his name of Gajasura. You can search about him.