system
Given the following post, respond as if you're commenting on Reddit:
human
[LOTR] Could Aragorn have just shown up in Gondor whenever he wanted and kicked Denethor off the throne and taken over? How did Denethor get such a position?
gpt
Originally there were two sister kingdoms, Arnor in the north and Gondor in the south. Both were founded by the High King Elendil, a lord of Númenor who escaped that land's destruction. After the War of the Last Alliance, Arnor was ruled by the descendants of Elendil's older son Isildur while Gondor was ruled by the descendants of Elendil's younger son Anárion. Both nations ran into their share of problems, with Arnor fracturing into multiple smaller kingdoms over succession disputes and having to deal with repeated invasions from Angmar, the domain of the Witch-king. Ultimately only one of the successor kingdoms, Arthedain, was able to maintain the line of Isildur. In the south Gondor managed better, surviving a civil war without completely fracturing and fighting off several invasions.

Eventually King Ondoher of Gondor died without a male heir. His daughter Firiel was married to Prince Arvedui of Arthedain, and they attempted to claim the throne. For one thing, under the old laws of Númenor, the oldest surviving child was the one who took the throne, regardless of gender. Besides that, Arvedui argued that Elendil had never intended for the kingdoms to be separate indefinitely, and that as the elder son it was Isildur (and his heirs) that were the true royal line in any case. The nobility of Gondor decided against these arguments, saying that Gondor was not in fact Númenor and that Gondor and Arnor were separate now even if they were originally united. They instead gave the throne to a great general and recent war hero named Eärnil, who was a distant cousin of Ondoher and therefore technically of the royal house. Not long after that, Angmar's forces completely overran Arthedain, and while the royal line survived the nation itself perished, and Arvedui's descendants lived on merely as "Chieftains of the Dúnedain". Ironically the royal line only lasted two more generations in the south. Eärnil's son Eärnur was baited into accepting the Witch-king's challenge of a one-on-one duel; he rode off to Minas Morgul with only a small guard and was never seen again.

The office of Steward had been around in Gondor for a while by that point, acting as counsellors, helping administrate the kingdom, and acting as a backup leader in the absence of the king. Because the line of Anárion was basically completely dead at this point, and because Eärnur was never technically verified to have died, at this point the Stewards took over rule of Gondor indefinitely. While they insisted they were only ruling "in place of the King", everyone knew the royal line was dead and that the Stewards were monarchs in all but name. Eventually we have Aragorn, a descendent of Arvedui and of Isildur before that. His eventual claim to the throne is basically the same as Arvedui's was, and it's likely that if he'd just shown up one day and demanded the throne that Denethor would have answered him similarly to how Arvedui was answered (i.e. "no, the line of Isildur has no valid claim to the throne of Gondor, go away"). But with Denethor dead, the remaining most powerful people in Gondor (Faramir and Imrahil) being supporters of Aragorn, and Aragorn himself having saved Minas Tirith and helped orchestrate the downfall of Sauron, the circumstances became much more favorable for Aragorn to claim the throne.