It all depends on whose perspective you take; generally there are three ways to determine who is the legitimate heir in Westeros.
Or as G.R.R.M. puts it:
The short answer is that the laws of inheritance in the Seven Kingdoms are modelled on those in real medieval history... which is to say, they were vague, uncodified, subject to varying interpretations, and often contradictory.
We can break them down into the following categories though:
1. Right of Succession
Right of Succession in Westeros is the normal way of determining succession.
- In general, Westeros follows the "Primogeniture" rights
- Males have precedence over females1
- Age is more than just a number (eldest is preferred)
- In Dorne (and perhaps the Iron Islands)
- It is the eldest child, then grandchild and so on
- Females are given the same preference as males, so eldest daughter will come before the next male2
- The Targaryen Inheritance way is also slightly different in that
- ALL males are given precedence, not just the immediate males (sons etc.)3
- Females only come into the equation if there are no males left in the entire line
- In other words, if there is any male - up or down the line - his claim will come before Dany's, regardless of age or how far the line you had to search
- See the Dance of the Dragons civil war for more info.
- This seems to follow the Male-preference cognatic primogeniture
- Extra reading on Rights of Succession in Westeros: https://scifi.stackexchange.com/a/37871/21267
2. Right of Conquest
Right of Conquest is the right to rule based on conquering and usurping the current dynasty. This is the Baratheon way4.
At this point, the successorship of the Iron Throne is so highly disputed that in my opinion, this is the one we need to look out for. The person with the biggest
dragons army will win.
3. The Great Council
The Great Council is a council formed when the succession is unclear5. They determine who should sit the Throne and their decision is binding. This is how "Egg" came to be king.
What does this mean for the claimants?
1. This puts Sansa in line to inherit Winterfell - ahead of any Karstarks or distant male relatives of the Starks. This is what Littlefinger is banking on for Sansa.
2. This puts Myrcella ahead of Tommen. This was the cause of the whole botched plan to crown Myrcella as queen.
3. This places Aegon VI Targaryen (Rhaegar's son) ahead of Daenerys.
Jon Snow and Bloodraven (had they not taken the vows of the Night's Watch) even come before her!
4. This puts Tommen as the rightful heir as he is the eldest surviving son of Robert Baratheon. It also puts Stannis Baratheon as the rightful heir since it is his claim that Joffrey, Myrcella and Tommen are not actually Robert's true-born children.
5. Assuming this could be formed to figure out who is the rightful heir given the following possibilities:
- Jon Snow as the next surviving son of Rhaegar
- Daenerys as Aerys' eldest surviving child*
*If Jon Snow and Aegon are proven to be false
Example Family - Starks
Ok, this is a bit muddy, let's take the Starks as an example and let's assume Bran, Arya, Sansa and Rickon are presumed alive:
- Normal Westerosi (Primogeniture rules)
- Bran is Lord of Winterfell
- Followed by Rickon, then Sansa, then Arya...
- Dornish Rules
- Sansa is Lady of Winterfell
- Followed by Arya, then Bran, then Rickon...
- Targaryen rules (if the Stark kids were Targs)
- Bran is Lord of Winterfell
- Followed by Rickon, then Harrion Karstark, then all of Harrion's male descendants, then Sansa, and finally Arya...