By the time Denethor was introduced into The Lord of the Rings, he had ruled Gondor as a steward for 35 years. A steward’s role was to be chief adviser to the throne, but he ended up ruling the kingdom as if he were king and denied the existence of any other heirs to the throne. So how did Denethor manage to reign for so long, and why did no other king take his place before Aragorn?
To understand why Denethor remained in command, one must first understand why Aragorn was the sole heir to the throne of Gondor. His people once lived on the island of Númenor until it was submerged in the ocean in the first war against Sauron. Migrating to Middle-earth, Isildur and his brother Anárion established the kingdom of Gondor, which they chose to rule together. Meanwhile, their father Elendil established his own kingdom in the north called Arnor.
Elendil being the father of the two rulers, he was made High King of Arnor and Gondor, and so every kingdom came under his control. This is how things went until the War of the Last Alliance. As seen in the opening of The Lord of the Rings: The Fellowship of the Ring, Isildur lost his father and was the one who defeated Sauron. His brother Anárion also fell in battle, leaving Isildur as the remaining king of the two kingdoms.
But being High King of two kingdoms proved too much for Isildur, so he entrusted Gondor to his nephew to rule. After this, Isildur led a journey back to Arnor but was ambushed by orcs and killed, leaving the One Ring lost in a nearby river. His son was left in charge of Arnor, and for many years to come the succession to the throne passed on the line of Isildur.
There was relative peace for a time until the Witch-King of Angmar rose to power, preparing Middle-earth for the eventual return of Sauron. The mighty Nazgul led armies into Arnor and utterly obliterated the kingdom, destroying everything in their path. As cities and people were destroyed, the king was able to survive and establish the Rangers of the North, the group in which Aragorn would spend his younger years before leaving for the events of The Lord of the Rings.
When Aragorn was a child, there was no kingdom of Arnor to rule, and Eärnur was the current King of Gondor. However, the Witch-King eventually made his home in Mordor and challenged Eärnur to single combat. The king agreed and left his steward Denethor in charge of Gondor awaiting his return. But in the end, Eärnur was never seen or heard from again, seemingly leaving the line of kings broken.
Aragorn being a descendant of the High King, he was the only remaining heir entitled to Gondor. But with the line of Isildur gone for many years, people assumed that the rightful kings were lost long ago. Gondor still had Denethor in charge, so many were content to leave him alone. And as we have seen during The Lord of the Rings: The Return of the King, he ended up believing that it was his right to govern. But eventually people realized that Aragorn was the heir, and he more than earned his place on the throne.