Gondor’s own laws and rulers even recognized how ridiculous Aragorn’s claim was. Arvedui, the last king of Arnor before he drowned in a shipwreck, once claimed the throne of Gondor, but the Council of Gondor rightly rejected him, saying the royal line of Gondor was descended from Anárion, not Isildur. Aragorn, like a many an illegitimate dictator before him, was only able to seize power due to the breakdown of law and society during the great crisis of the War of the Ring. Even then, with the doom of Gondor looming, Denethor the Steward of Gondor told Gandalf he wouldn’t bow to Aragorn, “last of a ragged house long bereft of lordship and dignity.” (Denethor may have been Middle-earth’s Worst Dad Ever, but he had a point there: Aragorn came from royal stock, but the only thing his family had administered for a thousand years was a forlorn wilderness full of ruins, wolves, and trolls that talked like Victorian gutter urchins.)
This is, of course the claim of monarchial legitimists, who seem to think that monarchy depends upon nothing but descent. They miss the key point, that monarchy depends upon descent from divinely-favored persons. Monarchy is sacral. Always has been, always will be.
And how does one determine divine favor? Chiefly, via heroic deeds and the exercise of actual power. The Carolingians had no legitimate claim to kinship over the Franks, but they had heroic deeds – the victory by Charles Martel over the Moors at Poitiers in 732 – and their role as maior-domus – the Prime Minister, who made the actual decisions. Thus, in 756, the Pope formally removed the Merovingian cipher from the throne in favor of Martel’s son, Pepin the Short. Pepin’s descendants ruled as legitimate kings for another 250 years.
Or Take William the Conqueror or Henry Tudor, who had tenuous claims to the throne of England but became kings anyway due to victory in battle and subsequent domination of the ruling classes. This method has been formalized into the concept of right of conquest, which implies divine favor.
On the other hand, you need only look at the poor case of Bonnie Prince Charlie to note that having a better claim means absolutely nothing. As the grandson by the direct male line of King James II, Charlie had a far better claim to the throne than the Hanoverian George II, against whom he lead a revolt. But Charlie was Catholic, and the people and Church in England decided they would rather have an mid-level German Duke with a collateral descent from Charles I, so long as he was Protestant. James II and his son and grandsons had lost legitimacy, and the Georges had gained it.
But what is lost can be regained. And the realm of Gondor had never replaced the monarchy of the Anarion line with the house of the Ruling Stewards, no matter how much time had passed. They seemed to operate under the unspoken premise that Kingship required the blood of Elendil, and kept the throne vacant “until the king returns” (the last king having ridden off to face the Witch-King and vanished). They denied the claim of the House of Isildur (Kings of Arnor and descendants of Isildur, Anarion’s older brother), true, because by that point, that house had indeed tumbled from it’s throne.
That meant that the people of Gondor needed only a hero with some connection to Elendil to fulfill the role of a king, and they would accept him as such. Which is precisely what happened. Thus, the Prince of Dol Amroth and the last Ruling Steward (Faramir, who inherited the office upon Denethor’s death) both proclaimed Aragorn King in a ceremony that Tolkien spent the better part of a chapter describing.
So Aragorn is precisely the legitimate King, the Messiah that Tolkien wrote him to be. It also means not to count out Danaerys Targaryen.