It sounds like the question you are asking in asking "how do I compose pieces like sonatas?" is "how do I compose?"
I agree with glenn in his Kurt Vonnegut vignette. The analogy he made is that writing composition is similar to music composition from the standpoint of you (the composer) starting with a blank page. And if brevity is the soul of wit, Kurt Vonnegut gave the greatest lecture of his life in saying that the best and fastest way to being a great writer is simply to practice writing, without further ado. I am in college now and if some of my teachers were more brief I would appreciate it
Sara gives some good advice on form.
Techneut talks about being schooled in music theory. So more on that topic:
I am interested in composing and e-mailed the composition professor at my university asking him if I could take his course (this was when I was a sophomore). He wrote back and told me I needed theory classes first.
The upshot of the e-mail was this- once you understand what a IV dominant and subdominant chord are, it is much simpler to approach what sequence of notes either melodic or melodic stacked to be harmonic can be justified to be heard in a piece of music over other note sequence options. So in short, you need an education in music theory before you approach composition, if you really serious about your interest in composition.
One last thing, I was reading Classical Music for Dummies by David Pogue last summer. He writes about the traits of a good composer. One thing he said that sticks with me is this. All good composers have something meaningful to say. Hope this helps!