Ok, have at me!
Heidi is a natural leader. To be happy, she needs to have a goal, be succeeding at it, and most of all, to have supporters and followers. Watch how she leads her sleepover guests in Marjorine. Now watch Skank Hunt/The Damned, when she reaches out to the only person available. Does she want to cry on his shoulder? No, she wants to give him a guided tour, teach him the lessons she'd learned, and educate him about vaginas.
As a natural leader, she's unhappy when no one follows her or she's not making any progress towards her goal. If she blames someone else, she'll take out her frustration by bullying them, like Butters when he fucked up her sleepover plans. If she has nobody to blame but herself, she'll break down, like in Douche and a Danish. She'll get better if she recovers some power and control.
Why did she take Cartman back after he tried to kill her? Because he accepted her leadership to change him (it wasn't just Cartman promising to go vegan - Heidi packed his lunch for him and used lunchtime to teach him about the foods she packed).
What made her give up on Cartman for good? Kyle confirming Cartman would never change, meaning that her Reform the Shithead project was doomed to fail and she was powerless.
Why did the girls upset her to the point of going back to Cartman? Because even though the girls weren't as shitty to her, she was powerless to change their behavior or get them to listen to her (she was completely outnumbered and they all talked over her). Even at his worst, Cartman paid attention and reacted to her, and he's only one person, which is easier to influence. And when she went back to him, his Jew theory gave her somebody to blame for her failures and an obstacle to overcome, which made her feel empowered again. Proven by how much initiative she takes in the next episode.
tl;dr Heidi is a natural leader, she needs to be in charge and successful to be happy, and she turns into a douche or a nervous wreck if she isn't.