In the absence of other materials, gravitational effects and electromagnetic effects, the light rays travel in a straight line. That is why it is called rectilinear motion of light.
The light rays are actually two fields: electric field and magnetic field perpendicular to each other vibrating all the time..
Light takes the path that takes the least amount of time to reach its destination from its source. If there is only one material in space , then it travels in a straight line. If there are two different transparent materials, then it gets refracted and alters its direction at the interface.
By traveling in a straight line, light takes the least amount of time to reach its destination from its source. This is the reason.