When I was a boy - in the 1950s - and was under the spell of Lonnie Donegan and his succession of fine guitar players, I dwelt constantly on what they played as obligato behind his vocal. I realised very quickly what they were doing - making up lines 'on the fly' that complemented the melody and the spirit of the song. I couldn't do it myself but, in some way, I understood the methodology behind it. I had no vocabulary of my own at the time but, through constantly listening to music, I slowly developed one.
I have always considered myself lucky to have grasped that fundamental at such an early stage. And Bob nailed it - '....you need to be able to play what you hear in your head'. Sometimes I'll 'hear' something that's too fast for me to execute but that's just a case of 'the flesh is weak' - I'm still working at that.