This is a fantastic question! There are a few important factors at play, but it should be mentioned that, on the whole, linguistics typically do not consider the differentiation between a "dialect" and a "language" to be productive. We typically lean toward terms such as "language variety" which covers both.
The issue is that separating the two into language and dialect of a language introduces a purely sociocultural hierarchy. For example, Appalachian and African-American English varieties (to name just two) are often designated as "dialects" of Mainstream American English (MAE). However, both varieties are equally efficient and productive within the communities which use them as MAE is within the communities which lean toward MAE.
The biggest aspect at the heart of the question, though, regards mutual intelligibility. This is still somewhat vague, as intelligibility and linguistic distinctness are each a spectrum. But it gets to the question of why we can confidently call Portuguese and Spanish separate languages, while the many varieties of Spanish are all considered the same language.
Things which we might call "dialects of the same language" typically will overlap in enough semantic and syntactic features that a concepts are accounted for in very similar ways. But, this isn't necessarily a sufficient rule, as many "dialects" are quite distinct. Again, the use of the term comes down much more to sociocultural and geopolitical ideologies rather than true linguistic differences. Consider that people (somewhat incorrectly) use the term "Chinese" to refer to all of the ten or so language groups in the country, many of which are not mutually intelligible. Conversely, Norwegian, Swedish, and Danish are considered distinct languages even though there is a fairly large degree of mutual intelligibility among these languages. In fact, the regional differences within the Scandinavian countries is often greater than the overall differences across the borders.
So, to answer the question, a dialect becomes a language when a community says it does for social cultural, geographic, or political reasons. Something "officially" being deemed a language can actually end up have very little linguistic reasoning behind it.