The idea that massless particles like photons can be affected by gravity seems counterintuitive, but it can be explained by the theory of general relativity, proposed by Albert Einstein. In general relativity, gravity is described as the curvature of spacetime caused by mass and energy.
Massless particles, such as photons, travel through spacetime along what are called "null geodesics." While they don't have mass, they do have energy and momentum. According to general relativity, the presence of mass and energy curves the fabric of spacetime, and objects (including massless particles) follow the curvature of this spacetime.
When a photon passes through a gravitational field, its path is influenced by the curvature of spacetime caused by the mass of the objects creating the gravitational field. In a sense, the photon is "falling" along the curved spacetime, much like an object with mass would fall under the influence of gravity.
This bending of light by gravity has been observed and confirmed through various experiments and observations, such as the famous solar eclipse observations that showed starlight bending as it passed near the Sun.
So, even though photons have zero mass, they still feel the effects of gravity because they follow the curvature of spacetime caused by massive objects. This is a unique aspect of general relativity that distinguishes it from classical Newtonian gravity, where only massive objects are affected by gravity.