I mean, if we take the C chord for example; it is composed by the notes C, E and G, according to the theory, but on the guitar, the pressed notes are C, E and C (an octave lower than the previous one), not to mention the other two strings that are also played (the 1st string and the 3rd). Why is that? How do chords work on guitar?
As you say,
G make a C major chord. But those notes can be played in any order, and they can appear multiple times, and still be considered a C major chord.
E G C is a C major chord;
C C G E E G is a C major chord; .... As long as there aren't any notes other than
G, it's C major.
This is true on all instruments, not just guitar. And it's true of all chords — as long as you're only playing the notes in that chord, they can be repeated and can occur in any order.
The C chord comprises C E and G . That's all. So any strings that make any of those notes will be up for grabs.
As you rightly say, some strings need fretting to make those notes, BUT others are already producing those notes while they're open. So why wouldn't they be left open to play as we strum that C chord?
OP mentions only 5 strings! A lot of guitar sites insist that chords need to be played in root position. That's nearly fair, but with that open C chord, the bottom (thickest) string already plays E - a component of the C chord - but could also be fretted on the 3rd fret, playing G, another part of the C chord, so that could be brought to the party, making the whole 6 strings work, either producing a 1st or 2nd inversion of the C chord.