I’m going to add some useful information about the guitar to the capable answers already provided. One of the characteristics of the guitar is that with the exception of the lowest 5 notes on the low E string and the highest 5 notes on the high E string every note on the guitar exists in at least 2 places on the instrument. Most middle register notes are available in 3 to 5 different places. As a matter of fact, if you have a 24 fret guitar the open high E note actually can be played in 6 different places! Of course some are much better sounding than others, for example the 24th fret of the low E isn’t going to sound very good.
What this gives you is the option to play a scale or anything else in many different ways by using one string or switching from one string to another. Some are more logical than others and this is something you learn with education and practice.
Let’s take your example of C D E F G. Here are 4 examples of ways those notes can be played on the guitar (there are several other ways to play this).
There is no right or wrong way and there are other ways as well. Usually the context or what comes before and after will determine which is best to use.
A guitar is tuned in 4ths, which is the equivalent of 5 frets. The exception is the G and B strings are tuned a major 3rd, or 4 frets apart. The 5th fret of the low E string is A, the same as the open A string. The A string 5th fret is D, same as the open D string, etc.
If you study the fingerboard diagram you used in your question you will notice something. Any note has its equal note either 5 frets higher on the adjacent lower string OR 5 frets lower on the adjacent higher string. (Except for the G and B which would be 4 frets). If this isn’t clear I will give an example:
Take the note E on the A string, 7th fret. That note is duplicated on the E string 12th fret (one string lower and 5 frets higher) and also on the D string 2nd fret (one string higher, 5 frets lower).
You can look for other examples on your own. This will help you learn the fingerboard little by little. It may seem overly complicated but in the end it actually makes sense because the sequence of notes is always the same, it just repeats in different positions on different strings.