What you want to shoot for is the ability to ask your keyboard player either, "What key is it in?", or preferably "What are the chords?", and then if you are armed with the knowledge I described above, he will never need to ask again.
Edit: I love this from Victor Wooten and it is universally true “You are never more than a half-step away from the right note…”.
Even without any knowledge of theory the right note is always just a half step away from the wrong note you might play. That means on guitar you can often bend your way out of sour notes as soon as your ears hear them. If you have a trem bar you can drop by a half or without a trem slide down by a half. Half steps are always 1 fret. Many great ear players I have played with do exactly this while soloing. They use a lot of chromatics and if their ear detects something wrong they just go up (or down) by a half step until it is right. If note length is short enough it will sound great. The trick is to avoid lingering for longer than say an 1/8th or a 1/16th note in atonal land. So just go up or down 1 fret to resolve the sour note and everything will sound fine.
So even if you do happen to land on wrong notes, the right note is always only 1 fret away! Victor’s suggestion is to practice the Chromatic scale when soloing. This will get you comfortable with hearing both the “wrong” and “right” notes and how you can incorporate them in your playing. The goal here is to not be afraid of making a mistake by playing a wrong note. Try to not be timid in your playing due to the fear of a mistake. Just learn how to detect it by ear resolve it by 1 fret without lingering on the bad note.
Which notes are sour goes back to intervals. But you can train your ears without understanding the theory behind intervals. If it sounds dissonant or sour go up (or down) 1 fret.