I do not know at which level you are . Here I give you the most general answer.
Gravity is not a force. It is a geometric property of spacetime. Space time fabric bends/gets curved due to the presence of a heavy object and this effect is known as gravity. This is the outcome of Einstein's General Theory of Relativity.
Now when a light beam passes nearby the space of a big star, suppose , sun, the path it takes in the curved space is the one which is called Geodesic. It is the shortest path and a straight one, the action integral (Advanced classical mechanics) is minimized for this path. If there were no bending of spacetime, the light would have travelled in a straight line but this straight line appears to be curved due to presence of a heavy object.