If photons are massless, then why are they affected by gravity?
Mass has nothing to do with gravity. It just so happens that mass correlates with energy and the energy itself spurs gravity. Photons have energy, therefore they have gravity. Newton's gravity equation is still useful for most gravity calculations, but the equation itself is not theoretically sound and it falls apart when relativistic or quantum physics comes into play.
Most high school curriculums still deem Newton's equation as "good enough" but using it for precise calculations is a Very Bad Idea.