Orochimaru is believed to have turned good because he realised the limitations of his own powers, in comparison to the adult versions of Sasuke and Naruto.
One of Naruto: Shippuden’s most consistent and recurring villains is arguably Orochimaru. The character was fashioned as a shinobi, and was driven by the pursuit of knowledge. However, his pursuit took a sinister turn, as he conducted gruesome experiments in order to fulfil his fascinations. His biggest fascination was attempting to learn all the Jutsu that ever existed. In order to have ample time, Orochimaru attempted to give himself immortality.
Seeking a vessel which would hold his form in his pursuit of immortality, he settled on Sasuke Uchiha as the perfect host. During the Chunin Exams, Orochimaru preyed on Team 7, which consisted of Naruto Uzumaki, Sasuke and Sakura Haruno. While Team 7 was in the Forest of Death, Orochimaru gave Sasuke the Curse Mark.
Subsequently, Sasuke came to join Orochimaru as he attempted to mould Sasuke into the perfect host for his body. However, he came to learn that Sasuke was not as weak as he believed him to be, due to the fact that the older he got, the stronger he became. This was much like with Naruto, who was his arch nemesis for taking away Sasuke. Therefore, by the time that the two main characters were in their teenage years, they had already surpassed Orochimaru’s strength and ability.
This is suggested as the reason that Orochimaru turned good. He realised the inadequacies of his own strength. This happened when he was faced with the Infinite Tsukuyomi and Kaguya’s wrath, and decided to fight for the good of the world. Thereafter, he took on a passive role of watching change happen, rather than attempt to instruct the change.