Some writers set out with a very clear goal: they want to write something that gets published. That might be a non-fiction book, an article, a novel, a short story...
Others have publication as a more distant idea: they primarily want to write, and they'll think about shaping that writing for public consumption at a later stage.
And, of course, a few writers don't want to be published at all. They simply enjoy writing for writing's sake – perhaps in a journal – and they have no intention of publishing.
None of these is "right" or "wrong". Plenty of writers want to be published because they feel their work won't be fully complete without an audience ... or simply because they want to have more time to write, so they need their efforts to pay off financially.
I would suggest, though, that you're unlikely to get very far with your writing career unless you actually enjoy writing! If you only write because you want to see your finished book on the shelves, you might be better off hiring a ghostwriter and focusing on the things that you do enjoy (maybe you love marketing, for instance).
And if you're writing just for the money ... well, I hate to break it to you, but there really are easier ways to make a living. (If you do want to make a living writing, I can definitely recommend freelancing as a more secure avenue than writing books.)
What if you enjoy writing and it's really important to you to be published?
Well, you could take the "write to market" approach: identify a good sub-genre to target and tailor your writing to it. Chris Fox's Write to Market: Deliver a Book That Sells is a great guide to this process, especially if you're planning to self-publish. If you naturally enjoy reading and writing a specific popular genre, then this could be a great approach for you.
Or what if you used to enjoy writing, but trying to get published is sapping all the joy out of it?
Some writers find that too much focus on publication (and sales) inhibits their creativity. If you're a novelist or short story writer, you may certainly want to angle towards ideas that seem like they will sell, but you definitely don't want to be second-guessing every character you develop and every plot twist you include. It's fine to write the story that you want to write
first, and think later about whether, and how, you want to publish it.
Ultimately, if your pursuit of the end goal – publication – is making the process of actually writing feel like a miserable chore, take a big step back. Have a week off from writing. Spend some time playing around with writing just for the sake of it. Abandon projects that you took on for all the wrong reasons. It's great to write just for the joy of it, at least occasionally; you don't always have to have a productive end in mind.
Equally, though, don't feel that focusing on publication makes you in some way a hack. Plenty of creativity and good writing craft goes into commercial genre books, so if writing to market is what you're drawn to, go for it.
Ultimately, it's great if you can enjoy writing and get published ... and it's also fine if you decide that one of those is more important to you, right now, than the other.