So, all theory aside, (I'm posting that in the music theory section), how does one truly create a piece (much less a three movement sonata) based on one melody or idea? How do you insert V7 chords in a way that make sense? How do you modulate into other keys without it sounding clunky and awkward?
You first need to have enough musical training that you can look at almost any piece of music away from the piano, and here exactly how it goes in your mind. This is called being able to "hear with your eyes" and it takes quite a bit of practice!