While not directly showed in the movie, it was strongly hinted that the story follows that of Warcraft lore:
Medivh (the guardian) was corrupted (or rather, possessed) even before birth by the spirit of Sargeras, the dark titan. This event happened when his mother, Aegwynn, fought and (seemingly) defeated the titan, around 800 years before the events depicted in the movie.
Sargeras' spirit remained dormant in Medivh, apparently occasionally taking control over his body to move his own plan of conquering Azeroth forward - this is why Medivh himself had no memory of letting Orcs into their world, among other things, only realising it towards the end.
His final transformation towards the end is when Sargeras seemingly takes full control over him, turning Medivh into a demon strongly resembling himself (or a dreadlord, as pointed out here).
In short, Medivh never consciously changed sides, he was merely being manipulated (possessed) by an entity far more powerful than him. Arguably, his true downfall was arrogantly dismissing this as a possibility, as he was likely aware of the possibility of it happening (shown when he burned Khadgar's drawings).