The idea that light has no mass is a result of our current understanding of physics. According to Einstein's theory of general relativity, gravity is not just a force that affects massive objects but also the curvature of spacetime caused by the presence of mass and energy. In this framework, all objects, regardless of their mass, follow the curvature of spacetime.
Light, although it has no rest mass, is composed of particles called photons that have energy and momentum. According to general relativity, mass and energy are interchangeable, and both contribute to the curvature of spacetime. When light passes through a gravitational field, such as that of a massive object like a star or a black hole, its path gets curved due to the curvature of spacetime caused by the gravitational field.
In a sense, you can think of the path of light as being influenced by the gravitational field indirectly through the curvature of spacetime rather than through a direct interaction with the mass itself. This effect is observed as the gravitational bending of light and has been experimentally confirmed through various observations, such as the bending of starlight around the Sun during a solar eclipse.
So, even though light doesn't have mass, it is still affected by gravity because gravity affects the curvature of spacetime, and light follows the curvature of spacetime.