How exactly does bariatric surgery lower the risk of getting cancer?

Unknown. Studies which have shown this correlation have not been able to find a clearly defined answer as to why people who have bariatric surgery seem to be at a decreased risk for some cancers than obese people who do not. It could be associated with the surgery itself, decrease in weight, changes in hormone levels, lifestyle changes following surgery, etc. And is likely a mixture of many mediators.
Doesn't. No evidence it does other than the association of obesity and cancer. Lose weight removes that factor- if it is a factor.
Hormone related. Especially in female gyn cancers (breast, endometrial, ovarian), estrogen levels can drop significantly with weight loss, thus decreasing risk factors for those cancers.
Inflammatory agents. Morbid obesity is the second most common preventable cause of cancer. Obesity significantly increases the risk of cancer death of many cancers. The mechanism is related to the inflammatory hormones and chemicals that are found in morbidly obese patients. Now, when we manage a very heavy patient with node-positive breast cancer, we suggest weight loss surgery shortly after the chemo and radiation.