This is among the first things we learn in a collage level study of Harmony:
This is the result of stacking the 'C' Major Scale in 3rd intervals (up to the Triad). Every Major Scale creates this unique (and identical) group of chords (by formula). The 'sound' of this grouping of chords must be absorbed into our ear's memory. We learn to identify the sounds (at first) by giving each chord a number (a 'Roman Numeral' that represents a 'sound' within a key - by formula). We call each numeral: a "Chord Function" (to connect the ear with the memory of the brain).
There are two points to be made here that are not necessarily explicit in the textbooks: The first is about easy memorization of formula: I, IV, & V are always Major Triads --- and --- II, III, & VI are always Minor Triads -- and -- VII is always a Diminished Triad.
The second point is about the ear: Any Major triad that we ever hear in our lives - will "most naturally" be heard as either: Ima, IVma, or Vma (in a Major key). --- and --- Any Minor Triad that we ever hear in our lives - will "most naturally" be heard as either: VImi, IImi, or IIImi (in a Major Key).
a Cma Chord (for instance) will be heard as either: Ima in the key of 'C' Major ('C' Ionian); IVma in the key of 'G' Major ('C' Lydian); or Vma in the key of 'F' Major ('C' Mixolydian).
a Dmi Chord (for instance) will be heard as either: VImi in the key of 'F' Major ('D' Aeolion); IImi in the key of 'C' Major ('D' Dorian); or IIImi in the key of 'Bb' Major ('D' Phrygian).
As for the VII(dim.) triad -- though it is rarely heard in popular music (by itself) - it is the only chord type of its kind within this group of 'Diatonic Triads.' It's uniqueness allows the ear the luxury of certain orientation (certain function within a key). Because it is unique -- the ear uses it to identify (without a shred of ambiguity) the key of its 'function.' It is the reason we see (for example) the '7' in a 'G7.' --- When we see a G7 in a chord chart -- that '7' is an indication of a Vma chord function. Whether we actually 'play' the '7th' tone (beyond the triad) depends on whether we want to hear the tone of 'Fa' (an active melodic tone) in the harmony as it progresses. [Take care not to confuse the '7' (a singe extension tone) and the VII(dim.) (a 3-note triad). For the purpose of this discussion - the VII(dim.) triad is thought of as being combined with the Vma triad. --- In the key of 'C' Major - this would be G,B,D (Vma) combined with B,D,F (VII(dim.)). The tones of the G7 chord are: G,B,D,F -- the 'F' note is the extension tone we call '7' -- and is the tone 'Fa' within the key].
This is keeping it simple -- focusing on the ear's response (and early training) to 'Diatonic Harmony' (harmony of the Major Scale). "Diatonic Harmony" is often referred to as "Modal Harmony" (the two terms are [virtually] interchangeable.