It's perfectly possible to keep one eye on the market, while still writing what your fluttering heart demands. It's not an either/or proposition.
The book(s) I've got coming out with Orbit next year started with me realising I was enjoying the trend of cynical/snarky anti-heroes. It sounded like fun to write one, so I did. (And it was fun). Am I writing hard-heartedly to the market? No, I don't think so. It's still my story, the way I wanted it, what my fluttering heart demanded. Just so happened that the market sparked the idea.
You can look at the market and think, ooh, actually I can do something with that....
Other than that:
Read
Write
Read more,
and analyse it
Write more
Observe the world and people around you, in detail.
Study what is selling, and while not writing
especially to it, keep the market, and the reader, in the back of your mind when you write.
Critique others' work (you'll find it easier to see problems in others work and then you'll see where you've done exactly the same in yours...). Also pays dividends when you need someone to crit your work. A writers group is great, if you can find one that meshes with you and where you are at.
Do not suffer from Golden Word Syndrome - be prepared to murder your darlings. With a chainsaw and napalm if necessary. You are not a special snowflake, or the gods' gift to the literary world (Probably anyway!
)
Remember you can
always still learn. There is no 'I have finished' to it. Learn, and keep on learning.
Remember there is no one book in the whole history of books that everyone loved. Develop a thick skin, and know that criticism of your work is not criticism of you. Even when it feels like it.
Then write some more. Always be working on one project or another (even if just daydreaming about the plot etc, which is the extent of my planning).
When it comes to querying
do your research. I cannot emphasise that enough. Make sure you aren't querying anyone dodgy (lots of scammers out there, and dodgy publishers too), that they have a good record. Have they sold, or are especially looking for, the sort if thing you have written? Before you sign, ask LOTS of questions, make sure you are
totally happy with them.
And cultivate sheer dogged persistence.