Both phonemes sound practically the same, so it's understandable that there are languages such as Spanish and Italian in which /j/ shares grapheme with /i/ in diphthongs. That is, in these and other languages in which /j/ is present, it does so as an allophone of /i/ in diphthongs. However, other languages with /j/ do have a separate grapheme for it (usually j or y). So how is it possible that there are languages that distinguish between both phonemes and others that do not?
First, it's worth noting that phonemes are entirely theoretical constructs. There's no objective, quantitative measurement someone can make to determine whether something is a phoneme or not; it's entirely at the discretion of the person writing the theory, based on what makes the theory more explanatory and elegant. This also means that phonemes are specific to a single language (and in fact to a single analysis of a single language), not universal.
That said, the main difference between
/j/ as phonemes is that one is a consonant and the other is a vowel. Phonologically, the definition of a "vowel" is generally that it forms the nucleus of a syllable, and a "consonant" doesn't. This means that there can be (near-)minimal pairs between the two, like in Latin: iambus
/i.am.bus/ "iamb" versus jam
/i/ forms a syllable of its own, and
Does this mean that they're always distinct phonemes? Not at all. In Hittite (according to Kloekhorst's analysis), for example,
[j] appears only before vowels, and
[i] never does, so it makes sense to call them allophones of a single phoneme. It all comes down to the specific language and the specific analysis you're looking at.
Are there actually any languages that actually distinguish between them?
It's an issue of semantics whether languages actually distinguish between that, or whether they distinguish between the number of syllables or moræ or some other unit.
I can't think of language that distinguishes between, say, /ia/ and /ja/ without a helping distinction such as that one is one syllable and the other two, or one mora and the other two.
Perhaps someone has a counter example — but is there actually a single language on the planet that distinguishes something like /ia/ from /ja/ that does not place both in a different syllable or mora, and thus can be said to actually be distinguishing syllable or mora-count instead?
But this is also your answer why they are distinguished: many languages develop to have meaningful concepts of syllables or moræ where the number of any in a word is consistent and altering this would change the meaning of a word, such a language would create a minimal pair between, say, hypothetically [ti.a] and [tja], for the latter has less syllables than the former.