Keys are a set of chords that follow a strict pattern. A musical scale has 7 notes and, to create the key, we build a triad (a 3-note chord) over each note of the scale. The major and minor scales have a specific set of intervals, which leads to the patterns we use most of the time.
Music that is centered around a tonic (the first chord of a key) is called tonal music. In order to establish its tonal center, a scale needs three intervals: a perfect 4th, a perfect 5th and a major 7th (also called leading tone). The perfect 4th and 5th give the scale stability, for those are the two most consonant intervals after the octave. Both major and minor scales have these intervals, so we don't need to change them. The major 7th (last note of the scale, only a half step before the first) is also important because it creates the tension needed to establish the home key. As the nickname suggests, it leads our ears back to the home key. This interval is minor on minor scales and that is the reason why we raise the 7th on minor keys, making it a major 7th.
The 3 main groups
If we have those 3 intervals in place, the home key can be established and the chords will then have behavior. That behavior, at the most basic level, has to do with moving away and moving back to the home key. The 3 main functions a chord can have are Tonic, Subdominant, Dominant. These functions are based on the 1st, 4th and 5th degree of the scale, respectively. Why these? Because they are the ones that never change, are consonant and give stability to the key.
Important! This is not to be confused with the names of each degree in a scale, where the notes are labled: tonic (1), supertonic (2), mediant (3), subdominant (4), dominant (5), submediant (6), subtonic (b7) and leading tone (7). The names of each function are derived from the labels of the first, fourth and fifth degrees in the scale.
At the most basic level, the chords in the Tonic group establish the home key; the chords in the Subdominant group move away from the home key and chords in the Dominant group move back to the key.
Assigning a chord to its group
We assign the chords into each group by similarity. At a fundamental level, a chord has 3 notes (also referred to as a triad). Two chords with 2 notes in common will belong to the same functional group. Our ears will perceive them as similar and they will have similar behavior inside their key. How strong that behavior is will vary between chords.
For example, in the key of C major the 5th degree is G. Therefore, the dominant group in this key will start with G major (G - B - D). The other chords in this key that have 2 notes in common with G major are E minor (E - G - B) and B diminished (B - D -F). For that reason, all 3 chords will be able to behave as dominants in this key. How strong that behavior is will vary between chords: the G major is the strongest, closely followed by the B diminished and E minor will be the weakest dominant chord of all three in this example.