As you probably already suspect, there is no secret formula as to how to get published. Nevertheless, my experiences have taught me a few things:
First, write a book that you'd love to read. Moreover, enjoy doing it. Then, no matter what happens, your time won’t have been wasted.
The second piece of advice seems to contradict the first: After writing a book you love, find people who will criticize it. Though praise is pleasant, that won't make your book better, and a book is intended for readers, not for you. If a critic doesn't get a point, don't assume he or she is stupid. The problem is more likely in the text. Rewrite. It's hard to find good critics. A few are toxic egotists, but most are far too kind.
Be persistent. It takes forever for a publisher to make up its collective mind. Be aware that all acquisitions are done by committee. Your book may have had advocates even though the acquisition committee rejected it. It helps to remember that rejections are the norm. Sixteen publishers thought they couldn't make a cent off publishing Harry Potter.
KEEP WRITING! My first publisher liked my first submission, but didn't buy it. If I had stopped there, they never would have bought my second novel. Always have your next book in the works. Writing improves with practice, and it keeps you sane. (At least, sane for a writer.)
Agents are useful, but ultimately, a book must sell itself. No agent can sell a bad book, and the good ones won't even try. I found a publisher first, and an agent later. A lot of my author friends have done the same. Agents prefer clients with track records.
Here’s one final bit of advice that I’ve found essential: if you wish to become a writer, keep your overhead low.