I like riding trains.

Literal

Train [to-に] ride [nominalizer-の] [subject-が] like.

The nominalizer の turns the verb phrase 列車に乗る ('to ride a train') into a noun phrase ('riding a train'), which then takes が as the subject of 好きだ. A core Japanese construction: X のが好きだ = 'like X-ing.' 好きだ takes が, not を, on what's liked — a classic learner stumbling block.