This is 3 questions in one, so it might take a slightly involved answer, or some good links.
I'm a very good classical pianist, but I skipped theory entirely in my early days and learned to play well by reading sheets and listening to better pianists online playing the same piece I was learning, then carefully emulating their sound.
But now I've made some friends who like to improvise and do it extremely well, and I'm at a loss. They usually take the low register and establish a beautiful rhythm going, while I take the upper register and toy around with a melody. I have no trouble keeping time with them, and I sometimes come up with very pretty melodies, but then here's what happens: I hit 2-3 notes and in my head I hear a melody continuing with the pattern of those 2-3 notes, but then I don't know what notes I'm hearing in my head (I don't have any sort of pitch, not even relative, I'm pretty much tone-deaf), so I can't continue the melody in real time because I don't know what keys to hit in order to create the notes I'm hearing in my head. How do you learn to know what notes to hit? Right now I just have to guess, and sometimes I get it, and sometimes I don't.
Problem two: while toying around with the melody, I'm pretty much trapped in single notes and octaves. Obviously in all the pieces I've played there are a million different chords of 2,3,4,5, even 6 notes, and they sound beautiful. But whenever I try to hit one, it just doesn't sound right. How can I learn which ones achieve what kind of sound and when they'll belong and when they won't?
Lastly: The way we do it is my friend picks a key and shows me what the key is, then I'm careful to only hit notes in that key. For instance, if she said F major, she would show me that B is flat (because I don't know my scales by name), and then while improvising I would be careful not to hit any other black keys and not to hit B natural, which are not in the scale. But I know from all the other pieces I play that you don't have to stay inside the scale in order for it to sound right-- obviously pieces have sharps and flats and naturals outside the scale all the time. But whenever I try to do it while improvising, it usually just sounds dead WRONG, instantly. But every now and again, by chance, it sounds so right. How do you know when it's okay to go outside the scale?
Sorry for long question!