If Tommen and Margaery both die, Stannis is the next in line for the throne (assuming he's also alive). If Stannis is also dead, then the answer depends on which medium you're talking about, (in the books, it's Shireen). Who's in line after Shireen is entirely unknown, because we don't know enough about Robert's family, but for practical purposes, it likely wouldn't matter.
Despite the fact that we know his real parents, Tommen is still legally a Baratheon. His position on the throne comes from his father Robert. As far as we can tell, the Seven Kingdoms use a typical male primogeniture system where the line of succession is basically:
- The eldest male child of the monarch;
- The eldest male child of the eldest male child of the monarch;
- The eldest male child of that person, etc.
Once you've followed the generations all the way down and run out of eldest male children, succession moves to that persons brothers, and then to that persons sisters. If there are none of those, you back up a generation and repeat the process.
So, Robert had two sons and a daughter. The line of succession was Joffrey, then Tommen, then Myrcella. (If Joffrey had given birth to a son, that boy would have been inserted before Tommen). If all three of those children are dead, you move up a generation, back to Robert, and go to the eldest brother.
This is exactly the reason that Eddard Stark claimed Stannis as the legitimate heir in the first place: if none of Robert's children are true-born, they don't count, and Stannis is next in line. If all of Robert's children are dead, then their legitimacy is irrelevant, and Stannis inherits anyway.
If Stannis is also dead, we repeat that process, starting from him as King; his only child is Shireen, who would become Queen.
Note that she's dead in the show but probably going to die in the books.
After that, things get very complicated. We would need to follow the family tree up to Robert's parents, who are obviously dead, and look for any uncles Robert might have had and track their children down. As far as I know, Robert's father and grandfather were both only children meaning it's going to take a lot of family-tree searching to find a legitimate heir. But at this point, if the line of succession gets this muddled, for all practical purposes it's basically ended.
People who are already close to the throne (particularly Cersei and Margaery's families) would probably begin to press claims for inheritance on their own. Normally a woman can't "marry into" the line of succession, Cersei can't be Queen on her own, but her daughter can, but when things get this murky, the rules tend to take a back seat to the people with the biggest armies.
In addition, this is the point where known bastard children would come into play. People like Edric, who is not in line for the throne legally but is Robert's child, become a huge political rallying point when the throne becomes heavily contested. They can make the argument that they're closer to the throne than anyone actually in the line of succession, and a lot of people would back them up.
So, if it happens that Tommen dies without producing an heir, the most likely outcome is various powerful houses (the Lannisters, Tyrells and the Baratheon's sworn houses w/ Edric Storm) going to war, again, until one of them has enough military and political support to simply declare themselves King and everyone will accept it and move on.