It's not a perfect system by any means. The police can and do plant evidence. But a lot of convictions also get thrown out and this is often why obviously guilty people get off on "technicalities" because the police don't follow procedure down to the letter.
The opposite end of the spectrum, not giving the police any power to do covert surveillance at all, is effectively just giving a license to the Mafia to do whatever it wants, as anyone who just commits all of their crimes privately will get away with it as long as they can sufficiently scare or kill anyone who would otherwise have been willing to testify.
It's a tradeoff either way, and we try to let there be a middle ground, where the police can bug your house, tap your phone, hack your router, but they need a warrant that has to be granted by a completely separate branch of government that is not in the same chain of command, and they have to prove to your lawyers that they obtained all of the evidence they have against you legally.