This was explained fairly early on in the first book of the series, so my recollection might be a little hazy, but I believe the Lannisters stood by and watched the war develop, unwilling to pick a side. Once they saw that Robert was inevitably going to win, they joined his side in order to maintain their power.
Basically, the Lannisters were willing to betray the Mad King to put Robert on the throne provided Robert marry Cersei, so eventually their son would be king. The Mad King did not like Tywin Lannister and basically screwed him over by naming Jaime to the King's Guard, so Jaime couldn't have a son and continue the Lannister line.
I don't think I'm giving anything away since this is back story but just in case I'll spoiler my answer. Please do not click on the spoiler if you think any of the back story and history will spoil anything for you. Spoiler Cersie was originally supposed to marry Rheagor the prince and next in line to be King. The Mad King slighted the Lannisters even though Tywin was his hand at the time and wed Prince Rheagor to Ellia who is the Red Vipers sister. You saw the Red Viper's conversation with Tyrian last week about his hate for the Lannisters and the Mountain. The Mad King was also very rude to Tywin at Tywin's wedding to his now deceased wife, making a comment that it's too bad the Kings right of first refusal with the bride was not still an honored tradition. Also, Tywin is no dummy, he was not going to go down with a sinking ship when he had an opportunity to come out on top. He was a great Hand but he hated the Mad King and the 1st opportunity to see him killed was also a power move to make the Lannisters the dominate family in the 7 kingdoms. Once Tywin turned on the Mad King it was in his best interest to have the Mountain kill Ellia and her child and any other Targaryan that could eventually seek revenge.
Not just revenge. Rhaegar and Elia's kids were murdered because they would be seen by many as the rightful rulers of Westeros. That's why all (or seemingly all) Targaryens were killed. A surviving Targaryen poses a threat to the crown because they have a legitimate claim to the throne. Viscerys and Daenerys were smuggled to Braavos so they wouldn't be murdered like the rest of their name. This is why Dany is such a big deal. Not only is she building her army to take back the throne, but many in Westeros are still loyal to the Targaryen's (Robert's Rebellion wasn't THAT long ago) and would presumably support her in the event of war.
Tommen Spoiler Unless you are from Dorne where the daughters are in the line of succession which means it could be Myrcella.