Shadow's change of heart traces back to Maria Robotnik. In his memories of the Space Colony ARK, she asks him to help people — to give the world a chance for a better future rather than to avenge her.
That promise sits under everything he does. He starts out pursuing his own agenda and clashing with Sonic repeatedly, but as the series progresses the wish to protect humanity takes precedence over his vendettas. The games frame it less as a single moment of switching sides and more as Maria's words gradually winning out.