Also depends on the tie that the rail it spiked into. Very similar to pounding a nail in wood, it can be very difficult to pounds a series of nails in absolutely straight line...at least I can't do it. The railroad have a certain amount of +/- tolerance that it has to follow.
The other is the tie itself could shift left/right with the vibration. Third is the level/contour of the roadbed. As I look at it it rolls (up/down).
As long as all this distortion is gradual, it usually isn't a problem.
Rails should indeed be straight, just as politicians should be honest.
You don't say where this is or when taken. My guess is that the weather was very hot, and the rails were the newfangled "seamless" kind without expansion joints every so often. That eliminates clickety clack but also eliminates expansion without getting out of alignment. That is shown well by the long lens. It gets above a certain temp, RR's will issue "heat orders" requiring trains to slow so as not to aggravate the problem with additional friction-generated heat which might make them go off the tracks.
Here's one of a cooler day on the UPRR Cal. coast line in November with the older clickety-clack rails. (D2X w/ 28-200 @28mm, F6 1/250 ISO 200.)
And sorry, did not mention the photographing conditions: it was freezing, and aggravated by a strong east wind, here in Switzerland some weeks ago. And the fog had lifted only about two hours before, meaning that the sun could not really be the cause for a local heating of the metal... I guess moreover that the rails are not of the seamless kind, but that I only have the effect amplified by a very long lens on a stretch of about 2000 meters of railway!
Yes, they should be straight even if they don't seem to go anywhere
I went out today after seeing your post and shot the attached railway bridge and tracks just for the fun/challenge of doing so.
A reasonably nice walk to get to the bridge from the highway when the wind was from my back but boy was it cold when I turned around to walk back to the van (about -30C with the windchill factor).
Doesn't help to explain the curves in your tracks but, as can be seen in the close-up shot of the bridge itself, the camera can play tricks. In my photo, the tracks appear to stop at the other side of the bridge.