In the season finale of Game of Thrones Season 5, Sansa lights a candle in Broken Tower. Later when Sansa is threatened by Myranda Theon kills Myranda and rescues her. Earlier on in the season an old woman approaches Sansa and tells her that her family still has friends in the North and that if she ever lit the candle, help would come. So I'm wondering, was Theon secretly in league with the Stark Secret Service or did he simply snap out of it just in time?
Theon snapped out of his 'Reek' persona when Myranda threatened Sansa. Until that point, he had been so beaten down by Ramsay that he was too terrified to assist Sansa. Remember, Theon watched Sansa get raped and reported to Ramsay when Sansa asked him to light a candle in the tower earlier in the season. Because of this, Ramsay flayed and killed the old woman you mentioned. His relationship with Sansa slowly brought him out of his insanity. First he risked Ramsay's wrath by telling her about Bran and Rickon. Later he helped her escape.
Added quotes from the creators of the show elaborating this.
The quote from Benioff:
We’ve seen terrible things happen to Theon and we’ve seen him refuse to rebel against his master time and time again, and finally, in this moment, he does, I think the great original sin of his life was turning on the Starks and betraying them, and he’s regretted it ever since. And now, finally, after all this time comes a chance for a little bit of redemption.
The quote from Weiss:
Both of them are people who have suffered so much at the hands of horrible people, but no matter how far gone they are — he’s as far gone as anybody we’ve ever seen — they can still come back and find the inner strength to keep on keeping on.
Just to add one thing about Theon which is explicit in the books and strongly implied in the show, which explains why it would be in character for him to act at that moment regardless of loyalties or plans.
By far his strongest motivators are terror and the belief that everything is a trap like his false escape with Ramsay always one step ahead - but Myranda's comment just before Theon acts temporarily changes this.
There are many moments in the books where he explicitly sees an apparent opportunity for a moment of redemption, escape, maybe even a quick less painful death for himself or someone he pities - but fears that Ramsay would somehow be one step ahead and find an even crueler punishment. Here's a typical example from A Dance With Dragons:
Theon drew the dagger. All I need do is turn and stab him [Ramsay]. The knife is in my hand. He knew the game by then. Another trap he told himself [...] He wants me to try kill him. And when I fail, he'll [...]
But in this particular moment, confronted by Myranda while Ramsay is out fighting, this terror and learned helplessness is less of an inhibition - particularly immediately after this line (taken from this transcript
[Myranda:] Ramsay needs you [Sansa]. Though I suppose he doesn't need all of you. Just the parts he'll use to make his heir, until you've given him a boy or two and he's finished using them. Then he's got incredible plans for those parts. So, shall we wait for him to come back or should we begin now?
First, Myranda gave away that she's acting alone. This isn't Myranda playing a part in another devilish trap of Ramsay's. It's just Myranda, acting on her personal grudge against Sansa, speculating about what Ramsay thinks. It's like an arachnophobe discovering that this isn't an overwhelmingly terrifying poisonous spider - it's just a quite frightening but beatable poisonous scorpion.
Second, Myranda discussed Ramsay's long term plans and the sadism he saves for his victims once they are no longer useful. It must have sent a chill down Theon's spine at the thought of what will happen to him if he reaches the day he's no longer useful.
One thing which seems to be common among long term abuse victims (including Theon) is being focused on just making it through each day, rarely thinking ahead. This comment of Myranda's will have bought Theon's future into sharp relief. No matter how good a Reek he becomes, always doing what Ramsay wants, one day Ramsay will still decide to do his absolute worst, with no warning.
Suddenly, it's meekly doing nothing and not rebelling which looks like the option most likely to result Ramsay doing his worst, while rebelling right now only involves dealing with Myranda.
His motivation to help Sansa had been slowly building, as described in the other answer - and this was one moment where temporarily, his overwhelming terror of Ramsay is actually a reason to act, and act now.