Note: The following applies to any instrument or group of instruments playing together. There's nothing about how chords work that is specific to guitar.
There's more than one way to use the word chord. One broad definition would be "two or more notes played at the same time". This question is really about a specific type of chord that we can call a diatonic triad. The word "triad" tells us that these chords are composed of three (distinct) notes, and "diatonic" tells us something about which three notes we will use.
Every triad has a note that it is based on, called the root. The root note of a chord is always part of the chord and it is also used for the name of the chord. So in your two examples, notice how the "A chord" includes the note A and the "C chord" includes the note C. The root of a chord is the "first" note of the chord and we usually think of it as being linked with the number 1. Sometimes you might hear or read someone referring to "the 1 of the chord", which is is just another word for root.
To find the other two notes of a triad once we have been given a root, we count up to "the 3" and "the 5". What that means is starting with 1 on the root, we count up note letters until we get to 3, which is the second note of the triad, and then we keep counting up to 5 until we get the third note. Using C as an example, C-1 D-2 E-3 F-4 G-5. Counting up from C, we see that a C triad uses the notes C, E, and G. If you look at your C chord on the guitar again I expect you'll find that you are also playing the open G string. We can form a D triad the same way, by counting up starting with D. That gives us D, F, and A.
By now you probably have at least two questions. The first is probably, "Why does my C chord on guitar have some notes twice? It goes C E G C E." When you build a C triad, it's important to have at least one each of C, E, and G. If you have extras of any of those notes, that's ok, it's still a C triad. One reason why you almost always play extra duplicates on guitar when you play a triad is that it makes the chord sound fuller and louder, and it makes the fingering easier, while still keeping it the same triad.
The second question might be, "Ok, so you're saying C is C-E-G and I know that A is A-C-E, so how does it make sense that C and E are in both chords but they are two different chords?" Well, it turns out changing just one note of a chord changes the entire name of the chord and the new chord has a different sound that we can clearly hear. So just changing the G to an A changes a C triad to an A triad. You can't really tell what kind of triad you have until you have all three notes, because any one note can totally change what triad you have.
And maybe by now you have one more question, which is a big one: "Wait a second, this A chord has a C#, not a C. And what's with all this 'major', 'minor', 'diminished', 'seventh' and all these other words people add to the ends of chord names?"
When you spell a chord (spelling a triad means to figure out the other two notes when someone gives you the root) as I've outlined above, using just note letters, the chord naturally has a certain "flavor". If we spell the chords based on the letters for the roots of the notes without accidentals from A to G, we get the following flavors:
- A-C-E is A Minor
- B-D-F is B Diminished
- C-E-G is C Major
- D-F-A is D Minor
- E-G-B is E Minor
- F-A-C is F Major
- G-B-D is G-Major
Notice we have three major, three minor, and one diminished chord. As to why they form that pattern, that's a topic that I don't think I can fit into a reasonable length answer here. For now just know that A) These notes are all the white keys on a piano keyboard and B) This is the pattern that was chosen hundreds of years ago for how the notes without accidentals work and there hasn't been a good reason to change it since then.
Many times, when the "flavor" of a chord is major, the word for the "flavor" is left out. So when someone says a "C chord" or an "A chord", they mean "C major triad" and "A major triad". Notice that if we just spell A-C-E, that is an A minor triad. You can turn a minor triad into a major triad by sharpening the middle note, called the third. So we spell A-C-E, realize that is minor the way it is, and we sharpen the third to C# to get the A major triad A-C#-E.
We can also turn a major chord into a minor one by flattening the third. So if you want to spell an F minor chord, you first spell F-A-C and then realize that's normally major, so you flatten the A to A♭ and get F-A♭-C for an F minor triad.
It gets a lot more complicated from there, and I didn't touch on diminished or seventh chords, but being able to understand the most common triads should get you started on understanding chords in general. One more note: If you want to spell a triad with a root with an accidental, start off with the same accidental on all the notes and then change the flavor as necessary based on the note without an accidental. Examples:
- E♭ major: E♭-G♭-B♭ is our base triad, but it's minor so we sharpen the G and get E♭-G-B♭.
- F# minor: F#-A#-C# is our base triad, but it's major so we flatten the A and get F#-A-C#.