Lopburi is on the main central rail line that runs in between Bangkok and Chiang Mai. It’s only about an hour train ride north of Ayutthaya. From Ayutthaya, simply get a ticket and hop on the train headed in the direction of Chiang Mai. Since the ride is so short, all you need is a basic third class seat which costs under 20 baht.
There’s something you should keep in mind about the timetable, though. I intended to ride the 9:48am train, only to be told that it was an express train that would not stop in Lopburi. I then had to wait a couple hours until around noon for the next local train. (Luckily, this still gave me enough time to see everything above.)
It would be wise to ask someone at Ayutthaya Station about this. There are still some web sites that say the 9:48am train does indeed go to Lopburi, so I can’t say for sure that none of them ever do.
Considering the layout of the town, Lopburi is much more doable as a day trip from Bangkok than Ayutthaya would be. You can ride there from Bangkok via Hua Lamphong station. The ride is around 2 and a half hours though, so try to get an early start.
If you want eventually want to head up to the ruins of Sukhothai and Kamphaeng Phet, you’ll want to take the train to Phitsanulok. From there you can take a bus to either of those towns. (Not many people realize, though, that Phitsanulok has some ruins of its own).