That comes from knowing your scales forward, backwards,sideways, up, down, etc. It also comes with an understanding…
Understand that when you are in G major and from a beginners standpoint are pretty much only going to use those notes of the G major scale no matter what chord you are in. If you are in G major and do a G,C,G, D progression then you still play those notes of the G major scale…but when you go to C start with a C note. When you go back to G start on a G note. When you go to D start with a D note…but stick with the notes within the G major scale. Those 7 notes.
With work you will see what notes are common in the G major scale, C major scale ,and D major scale. All 3 of those will share a lot of the same notes. You will know these common notes that all 3 share if you know your scales and notes…then you can use these notes.
For instance in G major and going to C - There’s no F sharp in C major like there is in G major…C major has an F in it vs the F sharp. So don’t play the F note because it clashes with the G. You can get by with it and that F shows up in bluegrass (blues). Using that F note in G major gives it a sad sound. Using the F sharp in G major gives it a happy sound. So C major has the notes C, D, E, F, G, A B. Note that G major has all the same notes besides for the F. Toss that note out and avoid it when you are in the C chord (and G is the major) C does not like the note F sharp, so avoid it when in the C chord. D loves F sharp and it’s in it’s scale and triad, so feel free to show that F sharp while you are in the D chord. Learn those notes.
I mentioned early that my brain is tracking and processing so many things at once. It’s actually fairly amazing to me, I mean it’s a feedback control loop that is tracking so many different things all at once and somehow manages to control all of those things. With me learning the fret board and a lo of music theory my brain does not have to devote so much time and effort into that like it use to…I’ve freed up space. At some point you will be able to use that extra brain power to get “cute” and jam with a melodic and creative spin on things.
The old joke is “She can’t walk and chew gum at the same time”. What if she practiced that for a long time? After a bit of practice she does not even have to think about walking and chewing at the same time, she can do both…but she has to practice.
There is nothing wrong with us…we just have to learn what comes a lot easier for some other folks. For those that it comes easy (er) to you will find them being “pros”. They just have a god given talent. We can join them but we have to work at it a lot harder.