How do Soul Gems get corrupted over time in Madoka Magica? After seeing how Madoka instantly transformed into her witch form after killing Walpurgisnacht in one shot in the 4th timeline, it makes me wondering: How do Soul Gems become corrupted?

I assumed that over time the magical girls would lose hope and fall into despair, which then clouded their Soul Gems. However, Madoka instantly turned into a witch, without any real reason to despair.
Soul Gems do indeed gradually darken over time, even if a magical girl doesn't do anything magical. I'd imagine this is because the gem still needs to magically sustain her body, or something like that. But they also darken for other reasons - the emotion of despair causes a Soul Gem to darken, and so does the use of magical powers. 

In the 4th timeline, after defeating the Walpurgisnacht, Madoka had no reason to despair... but she did expend an enormous amount of energy to defeat the Walpurgisnacht in one shot. Apparently, that was enough to cause her Soul Gem to blacken completely, thus resulting in her becoming Kriemhild Gretchen.