Several different factors go into it.
Like most people, I keep a notebook/file of ideas that interest me for potential future projects. I write a lot of historical fiction, so I need to plan research time into my writing process.
I only work on one project at a time to keep myself focused, but when I've finished a work I open up my notebook and flip through my ideas and read through them all, to see which ones interest me the most. Then I hit the library, book store, and internet to read up on the ideas I've chosen.
When I research a broad subject, I keep an eye out for potential stories. Interesting characters, mysterious unknown factors to this particular bit of history, etc. I just do a ton of reading and start to play with ideas for stories in my head as I continue to research.
Eventually, an actual story will start to gel. Then I begin outlining.
However, sometimes there are reasons to work on a particular idea right away instead of going back to my notebook. I'm querying a novel set in Egypt, and there aren't a lot of those on the market, though readers have been eagerly buying the few that are in print. So I'm working on another one, and drawing up outlines for a few more just in case my future agent wants to know what else I can come up with for this setting. So, sometimes I make certain projects a priority based on opportunities I believe may be coming.
My current WIP is a short story that will end up being about 6,000 words. I got the idea for this one from a totally random phrase that popped into my head, apparently out of nowhere: "John Muir f***s a robot." The idea was so funny and intriguing to me that I just had to play around with it some more. I decided to turn it into a respectable alternate-history/sci-fi story. It'll need a better title, though, when it's done.