I usually study the compositions of great composers (i.e. my last one I studied Haydn and Beethoven, this one Debussy and Scriabin). After watching their writing style, ideas begin to pop up in my head. Then it's just a matter of writing things down (sometimes it takes hours for even the smallest melody) and improvising new themes off of that one. Usually what I end up writing is either a theme and variations or a sonata (usu. 3 movements).
Then again, sometimes I'll leave an idea or fragment unfinished, so that I can come back to it later.
Example: I wrote a piece in Bach's baroque style in D minor about a year ago. That piece was unfinished, but it had a few interesting and good melodic ideas, so I kept it. A few weeks ago I finished a set of 13 variations and an Arietta on Bach's Minuet in G. The variations aren't much, but the Arietta (which is completely original, but I put it at the end of the piece because it seemed to fit) is very beautiful. The baroque ideas are now part of the 3rd movement, and the Arietta the 2nd movement, of my latest creation: Sonata No.2 for Piano and Violin in C-sharp minor.
It helps to have a library of random ideas you write down (or a sketchbook, like Beethoven.)
Phil