Some writers might be able to give you a straightforward answer to this question. But I’ve never met one that really could. For most of us, it’s as varied and unpredictable as dreaming . . . there are elements of real life, elements of fantasy, mix-ins from things we thought were neat, ideas that have been interacting with each other since who knows when. I have no idea where my ideas come from. They seem to sort of create themselves.
The dream analogy was actually a very good answer!