When Frisk enters Waterfall in Hard Mode and meets with Sans, the glitched state of the timeline allows Gaster to will himself back into existence. He then attempts to take Frisk's soul, forcing Frisk and Sans to fight back against him.
Due to the glitched state of the timeline, the previous reset by Sans causes Chara to return and try to steal Frisk's Determination, forcing Sans and Flowey to team up to stop him.