He wasn't from the direct line of the kings of Gondor, but he was from the direct line of the kings of Arnor. Since, as you say, there was no heir to the throne in Gondor, and further since one of Aragorn's king-anestors married the daughter of one of the earlier Gondorian kings, and lastly...