In general music theory a chord is simply a set of two or more specific notes. So the examples in the OP are a G chord (made up of the G, B and D notes) and an E chord (made up of the E, G# and B notes).
How are such chords translated onto a guitar fretboard? This can be done in various ways, but the core principle is that each string played or strummed should be one of the notes that makes up the chord. For example, the following guitar tab lists several chords. All of them are valid G chords, but they are all slightly different. (To read the chords, the horizontal lines represent the strings. The first line is string 1, down to line 6 which is string 6. The chords are read vertically and the top line is a numeric reference for the related comments after the tab.)
Chord# 1 2 3 4 5 6
Chord #1 is the tab version of the first diagram from the OP. The OP asks a good question: "How is it possible for chord to have more than 3 notes…if the chord is a triad?" In one sense this is a six note chord because it includes all six strings of the guitar. But in another sense it is a triad because only three distinct notes are involved. G is played on strings 1, 3 and 6; B is played on strings 2 and 5; and D is played on string 4.
Chord #2 is the same as #1 with one exception. On string 2 the note played is at fret 3, which is a D instead of a B. This is known as a different voicing of the chord. The same three notes are involved across the six strings, but there is a slightly different balance. We have gone from two Bs and one D to the opposite, one B and two Ds.
Chord #3 is a chord played on only three of the strings. It now matches the three notes in the theoretical G chord: G on string 6, B on string 5 and D on string 4. Strings 3 to 1 are not played. This chord might be either strummed or plucked according to context.
Chord #4 is another example of a three string chord, but now the notes are played on strings 5, 4 and 3. The chord is still a G chord as with #3, because the same three notes that make up a G chord are present. But the order of notes is different, and this difference is known in chord theory as an inversion. the inversion is defined by the lowest pitched or bass note. So #3 is the first inversion of a G chord (G-B-D); #4 is the second inversion (B-D-G). The third inversion is D-G-B, and is tabbed as chord #5.
Chord #6 is another voicing on a G chord. It is a barre chord, where all six strings on the guitar are fretted. So it is another voicing of a G chord, along with #1 and #2. There are other voicings of most chords up and down the fretboard.
A couple of final comments. First, it's important to take time to understand how a chord is drawn in any particular resource. Unlike classical sheet music which has a reasonably standard language, guitar resources are quite variable. In the current instance, chord diagrams can be drawn very differently in different books or online sites. We see that variation in the two G chord diagrams above. Both diagrams have recommended fingerings. But the fingering for diagram 1 is at the foot of the string lines, while in diagram 2 the fingering is closely beside each circle. It's easy to misread a chord diagram if we don't take care to check the guidelines first.
Second (and perhaps related to the previous comment) is this from the OP:
Notes are: E B G whereas according to the scale E F# G# A B C# D# E it should be E G# B.
It is correct that an E chord is constructed from the notes E-G#-B. But it is not correct to read the diagram as E-G-B. The third string is the G string, but the finger is fretting that string on fret 1, so you will be correctly playing a G# note.