How do locomotives know where to go?

a train traveling down the tracks with black smoke coming out of the chimney

They have a lot of training.