The ice generator in snowdin is built to drop ice into the river, and the ice flows to the core, making it so it doesn't overheat. If it overheats, then it starts to break down.
So the ice machine broke one day.
Sans and gaster were in the core. They both felt the core start to rumble. The ceiling started to collapse, and sans and gaster needed to evacuate as soon as possible. Gaster noticed one of the large metal pipes in the ceiling start to break. It was about to hit sans, so gaster pushed him, putting himself in danger. The last thing gaster said was "dont forget-" and then he was hit, knocking him into the core before sans could do anything. Sans, being only 12 at the time, didn't know what to do
Gaster's soul had aborbed so much magical energy, it caused him to destabilize, and his soul to crack. The only people who remembered him were river, his followers, chara's ghost, and sans.