First of all, Chris Fox has a whole book and other stuff talking about writing to market. If you want the deep dive, go there.
Here’s my very down and dirty explanation of how to write to market:
Read all the most popular books in your genre.
Read the classic books in your genre.
For me in fantasy, the most popular are books by George RR Martin, Brandon Sanderson, and R. A. Salvatore. The classics are Tolkien, T. H. White, Marion Bradley, and Fritz Leiber. There are more, of course, but that’s my short list.
Analyze all those books you just read. Take notes on them. What makes them enjoyable? Why do people buy so many copies of these books every single day? Figure it out.
Read the reviews of those books. What don’t people like about them?
Use all that knowledge to develop an understanding of the tropes in the genre. You don’t want to write every single trope you can possible fit into a book, but you need to have the major ones.
Find your unique spin within the major tropes. If you write something completely 100% original and never seen before, firstly that’s impossible, but secondly it won’t sell. People won’t like it. But you also can’t churn out a direct copy of something well known and expect people to support it. Find a middle ground. My Goblin Wars series has the tropes of an epic hero with a magical sword, a grand quest across the land, and an interesting duology of gods. That’s all standard fantasy. What makes it unique is the MC is a goblin, the goblins are a hivemind, and humanity is the minority among the fantasy races. They only have a single city, and all the other races far outnumber them. That stuff makes it really unique and interesting, but the main tropes are still there so people feel comfortable.
Get involved with your fanbase so you can follow tropes. Watch the forums. Attend the conventions. Listen to interviews with big name authors. You get the idea. Follow the tropes by following the fans. Learn what’s getting hot and what’s getting cold so you can adjust accordingly.
If you follow the fanbase, you can follow the microtrends. About 5 or 6 years ago, the subgenre of LitRPG became stupidly popular. I was following fantasy intently, so when I saw it gaining a lot of traction, I read the major players, learned the style, and wrote my own. Those books are my all-time best sellers now.
What writing to market isn’t.
Don’t jump genres. I see this a lot. A sci-fi author, for instance, will bemoan their lack of sales and complain that romance is the hot genre. Guess what? Orson Scott Card sells a shit ton of sci-fi novels. And they don’t have much romance in them at all.
Stick to what you know and what you’re good at writing. Don’t jump genres entirely just because that genre sells more. Find out how to sell more within your specialty by following microtrends.
A lot of people think writing to market means selling out and sacrificing your “artistic vision” or whatever. I’ve never had an artistic vision, so I don’t know about that, but writing to market simply means tweaking your writing to fit the market’s expectations better. It doesn’t mean reinventing your entire author brand into something disingenuous.