There is no origin story for Ganesha's elephant head. There are several, they are mutually exclusive, and the Puranic texts that carry them do not agree.
The best-known version has Parvati create a boy from the substance of her own body, or from clay and turmeric paste, and set him to guard her door while she bathes. Shiva returns, is refused entry by a child he has never seen, and beheads him. Parvati's grief is such that Shiva sends his attendants out with instructions to bring back the head of the first creature they find, and they return with an elephant's. In other tellings Shiva creates the boy directly. In others again he is born with the elephant head and no beheading occurs at all.
These are not variants of one story with details drifting. They disagree about who made him, whether he was ever beheaded, and whether the head was ever anything else. A tradition that had one account and forgot it would not produce this pattern. A tradition assembling explanations after the fact for an image it had inherited would.
Behind him stand the vinayakas. In the Yajurveda these are a group of four troublesome spirits who create obstacles and difficulties and who are easily propitiated, and Krishan traces Ganesha to the four vinayakas of the Manava Grhyasutra, calling him a non-Vedic god. The name Vinayaka carries forward as a standard name for Ganesha in the Puranas and in the Buddhist tantras. The shift is from spirits who cause obstacles and must be bought off to a single god who removes them, and it is not hard to see why the second is a more attractive proposition than the first.
Scholars working this material include Paul Courtright, Anita Thapan, A.K. Narain, M.K. Dhavalikar and Robert Brown, and they do not agree with one another either.