There is nothing out in space to prevent the sun's light from going infinitely far, in principle. In practice, the sun is only 4.5 billion years old, so its light can only extend 4.5 billion light years away from us right now. But there's nothing to stop that light from expanding outwards forever, as time goes on. The sun's light takes 8 minutes to reach the earth, which is within the solar system. The Solar System compared to the Milky Way is 0.0032% the size of the Milky Way. Now just imagine how long it would for the sun's rays to go outside the galaxy which is soo soo huge in comparison to the solar system.
So, to answer your question, the ray of sunlight can go outside the galaxy. There is nothing preventing it from doing so. But it would take a duration of time so large that it cannot be possibly measured.
Hope this helps :)