There are only a couple known non-malicious causes of double baking or endorsing.
The first is running two bakers and/or two endorsers with the same wallet at the same time. Some bakers build their infrastructure to be highly available and have multiple bakers and/or endorsers running. There had been a couple cases where a switch over had caused double baking. To avoid doing this, you can either totally avoid having the possibility of running two bakers and/or endorsers at the same time. Or you must test your setup thoroughly. In my opinion, it's better to miss a couple baking or endorsing opportunities than risking a double bake/endorsing.
The second known cause is via third party baking app like BakeChain. BakeChain had a race condition that was causing multiple bake operations to fire unintentionally. So be very selective with what tools you choose to use to bake.
An extra layer to further help avoid double baking/endorsing is to use a mechanism called double bake protection. The most common and well known implementation is via the ledger baking app by Obsidian. The ledger baking app itself prevents the ledger to sign an operation on the same block number twice. Therefore even if you run two bakers, a single ledger will not sign the same block twice. Another double bake protection implementation is the remote-signer, it is also what the foundation bakers uses. Remote signer does require a hardware security module(HSM) to use and it's a much more costly solution.