Unspayed female dogs are actually at a higher risk of mammary tumors (breast cancer) than spayed dogs. As with any type of cancer, there is no guarantee that an unspayed dog will develop it or that a spayed dog will never develop it.
Here you can read more: https://m.petmd.com/dog/conditions/cancer/c_dg_mammary_gland_tumor
Benign and malignant tumors of the mammary glands occur fairly frequently in unspayed female dogs, in fact they are the most common type of tumor in the group. Spaying can largely reduce the risk of developing this type of cancer, especially if the dog is spayed before it has an opportunity to go into heat.
A genetic basis is possible in some breeds, and there are frequently some genes that can be identified in dogs that are predisposed to cancer of the mammary glands. For example, [...] German shepherd dogs, Maltese, and Yorkshire terriers have been reported to have an increased risk of developing breast or mammary tumors compared to other breeds.
Edit
Mario is right, the actual act of mating or reproduction has no influence on the risk of cancer. Dogs in heat produce a lot of hormones. In females these hormones often cause the production of milk, even if she didn't mate. That is probably what causes the breast cancer.
Spaying a female dog prevents the production of hormones, so she doesn't go into heat anymore.