I know in WV, I have used them often and have talked to railroad workers doing work and they have never said anything that would suggest to me that it was illegal. But not 100% sure
I would assume if it was on public or even private land and you aren't obstructing the railroad you would be fine.
I believe if you're walking on the tracks - it's considered tresspassing because they're privately owned. But, you could walk beside them and not have any problems. However, with that said - I live in PA and I've walked the railroad tracks in to game lands numerous times and never had any issues.