I just came across this old yet interesting article about the improvisation stories of certain classical composers. Since classical improvisation back then was a huge common practice and needed to be taught, how did classical composers such as Bach, Mozart, Beethoven, Chopin, Liszt, Debussy exactly practice piano improvisation? How are they so good at making melodies out of thin air?
If you do some digging, you will find a lot of material on this topic.
Try these for starters:
Callahan, Techniques of keyboard improvisation in the German Baroque and their implications for today's pedagogy. http://hdl.handle.net/1802/14278
Sanguinetti , The Art of Partimento: History, Theory, and Practice. http://a.co/euEKekm
Gjerdingen, Monuments of Partimenti. http://faculty-web.at.northwestern.edu/music/gjerdingen/partimenti/aboutParti/index.htm
When you hear stories about composers who could improvise fugues, they are talking about partimenti fugue.
Also, check out these videos:
- Robert Levin: Improvising Mozart.
- Improvising Mozart with Robert Levin.
The same way as the rest of us, I'm afraid. There us no magic to learning this skill. Music, like any area of study, is full of patterns. Neurologically speaking, as you observe more patterns and get better with them, you can utilize them faster and with more aptitude/facility.
One of the theories for how Bach hurt his eyes was by spending his youth copying music by candlelight as a form of study. In Jazz school and in conservatories, you transcribe works as a form of ear training and study.
A good, healthy mix of playing the music of others, studying that music very closely, constantly trying to improvise, and writing out your own music are all important parts of learning to improvise.