I love this topic!
Well actually It depends on a situation a lot.I can get a good idea while walking through a forest or a city and just record it to a dictaphone by simply humming the melody.
Also I sit down the piano and just play something until I find a cetrain idea in my playing which I actually like and I start to wonder what it is about and where can I go with it.When I have a good melody or a motif I think about it all the time.It just sounds in my head and I can hear it developing further.Sometimes It takes so long to finish something because I'm always reworking and remaking it until I'm satisfied.Usually when I comose something late at night,I record it and then the next morning I listen to it and make a statement.It always feels very different.
And I also get inspired by sounds.When I play the guitar I'm just inspired by the sound of the guitar and it helps me get different ideas.Same with string sections or brass sections.
And sometimes I can listen to a piece of music which I like and it inspires me to compose something similiar.
So it depends really on the situation.Either I can get good ideas in my head and either I can get them by playing the instrument