In a certain sense, light always
takes the "straightest possible" path. (Massive particles also do this). But the presence of a massive object curves
spacetime, and that changes those "straightest possible" paths.
When a ray of light passes a massive object, it will take a path that isn't a straight line in the 3-dimensional slice of spacetime that we think of as "space, at this moment in time". But the path is
a straight line (technically a "geodesic
") in spacetime.