I don't believe it is ever clarified when Tywin finds out about Shae.
There are a number of possible explanations that come to mind:
Tywin's spies managed to track her down. Tywin certainly does not trust Tyrion, and it seems certain that he would have made efforts to keep an eye on his activities. Tyrion went to a lot of effort to hide her presence, but he started to get sloppy as his feelings for her grew. He knew that bringing her to live and work in the castle was a bad idea, and it may very well have been his (or rather, her) undoing.
Shae may have been working for Tywin since the very beginning. She was introduced to Tyrion by Bronn, who claimed to have stolen her from another knight in Tywin's camp. I don't believe Bronn ever revealed which knight, and when Tyrion asked Shae herself, she avoided answering directly, only saying that he was a "small man" and a minor figure. It is possible that this was a lie, and that she was really "taken" from Tywin at the start. It is even possible that Bronn was knowingly acting as an agent for Tywin in delivering her to Tyrion; he was unapologetically mercenary, after all. If this is the case, then Shae would have been deliberately planted by Tywin as a spy, and returning to his bed once Tyrion was arrested would have been natural.
Varys may have revealed the truth, since he knew about Shae, and was convinced to provide very damning information as part of the testimony against Tyrion at Tyrion's trial.
Of the three, I think Tywin's spies finding out about Shae is the least likely; Tyrion is reasonably careful, and while Varys found Shae despite Tyrion's efforts, Varys' mastery of spying makes Tywin look like a child by comparison. Once Varys entered into his alliance with Tyrion to help hide Shae, it seems very unlikely that any spies employed by Tywin would find out about the secret.
The possibility of Shae originally working for Tywin originally, and then being sent to Tyrion as a spy, strikes me as very plausible. Clearly Tywin was not above sleeping with a whore, since Tyrion found her naked in his father's bed.
Still, my personal opinion is that Varys revealed her to Tywin. In fact, I believe that the whole trial, and the subsequent events, were part of a deliberate setup by Varys.
Varys testifies against Tyrion. Yet it also seemed too easy for Jaime to convince Varys to help Tyrion escape. Tyrion goes to Illyrio, Varys' partner, and then subsequently sent with "Griff" and "Young Griff" (later revealed to be Jon Connington and Aegon Targaryen) to support Daenerys Targaryen. If this was Varys' goal, then providing evidence at the trial would be an excellent way for him to force Tyrion to flee from King's Landing, and to go where Varys wanted him to.
Arranging for Tyrion to find Shae in Tywin's bed (notice that Varys doesn't argue too strongly against Tyrion going to find his father, and quite willingly provides directions on how to sneak in to his quarters) would potentially serve a double purpose for Varys: it would set Tyrion even more firmly against his father, encouraging him to support the Targeryon claim, and it might also prove an opportunity to remove Tywin from the scene altogether.
The fact that Varys later murders Kevan Lannister, and then explicitly explains that he did so to destabilize the Lannister rule, sow dissent between Lannisters and Tyrells, and therefore making the kingdom an easier conquest for the returning Targaryens, makes it quite clear that Varys would have needed to eliminate Tywin somehow.