While attacking Konoha, Obito was caught off guard when Minato recognized him. This gave Minato the chance he needed to stop him. After knocking Obito out, he sealed his chakra off and he was taken to cell to await sentencing.
Minato decided that instead of killing his student, he would try to change his heart. So he had Obito do community service when he wasn’t held in his cell.
Kakashi, being as hopeful as Minato, would visit Obito daily. Desperate he would have long talks with him, sneak him treats, and spend whatever time he could with him. Sometimes when no one was watching he would help Obito with his community service, regardless of how nasty or hard it was.
Over time Obito’s resolve began to shift, but when it did he began having severe chest pains. Medics looked at his heart and found the Justu placed by Madara. They were able to remove it but in the process realized what the boy needed was mental health treatment. He was then forced to see a professional every week.
With the jutsu gone and the care he received from Kakashi, Obito started to change. When it was decided that it was safe, they let him live in the village in his own home. However, he had to continue seeing a professional every week and report to Minato several times a week.
As a free man he sought out anything and everything to take away the emptiness in his soul. From alcohol to sex to cigarettes, anything that could make him feel anything other than pain. This ended up getting him into trouble when a drunken night turned into a fight. He was put in a jail cell to sober up and then given a warning that if he didn’t clean up his act he would go back to his original cell.
It wasn’t until Kakashi revealed he was losing faith that he would ever have his old friend back, that he realized he actually had to change. He had already lost Rin, he couldn’t handle losing Kakashi after all they’ve been through.
Obito began taking his mental health treatments more seriously after that. He started cutting back on the self destructive things he was doing and started doing his best to be his old self. He worked hard to change for Kakashi and over time he started to see the fruits of his labor.
After spending a year with a clean record and proof he was changing, Minato removed the seal on Obito’s chakra. He was occasionally allowed to go on missions as long as he had someone there to keep him in line.
Now he is finally at a place where he can live a relatively normal life. He works hard to keep himself in check and does his best to be his old self.