New writers ask this question all the time.
The direct answer is: Everything you write is automatically protected by copyright the instant you write it. To have legal proof that you really wrote it, you should register your copyright with the copyright office in your country. Last I checked, in the U.S., to register a copyright of a book costs $35.
I've heard lots of new writers talk about schemes to prove they wrote the book without registering the copyright, like mailing a copy to yourself in a sealed envelope or leaving a copy with a lawyer. I don't see the point with most of these schemes. Lawyers typically charge $100 an hour or more. It would cost you way more than $35 to get a lawyer to safeguard your book. Mailing a copy to yourself is easily scammable: as someone else on here noted, how would you prove that you didn't mail yourself an empty, unsealed envelope and put something in it later. On top of that, there are specific provisions in copyright law that give you additional protection if you register the copyright. Nothing in copyright law says you get these extra protections if you give a copy to your lawyer or sign a copy with your own blood or sacrifice three chickens to the Great Novel God or whatever scheme you've come up with. Just pay the $35 and do it legally!
In any case, the odds that someone is going to try to steal your book are tiny. Yes, it happens, but someone is far more likely to try to steal your car or pickpocket your wallet. In both cases, I take reasonable precautions. I don't leave my car unlocked with the keys in the ignition. I don't leave copies of my manuscripts lying on the table in the coffee shop and go home.
If you were an established, famous writer -- J. K. Rowling or Steven King or some such -- maybe there'd be people out there trying to hack into your computer and steal your book. But if you're an unknown, they're just not trying. They've never heard of you, and even if they had, they have no idea if you're any good.
Don't waste time trying to protect your writing from thieves. Spend your time writing and polishing your work until you have something that is good enough that someone might want to steal it.