As far as Im aware most of the steam engine parts back then were made in cast iron so how could they build steam piping systems that were airtight?
Yes, many parts of steam engines themselves were made of cast iron- malleable cast iron was and is easily machinable, and for complex parts like steam chests and steam cylinders, it made the most sense.
Boilers however were a different matter. Cast iron was brittle and could crack, if thin. A boiler was worked up from more ductile iron plate and sheet iron. The main seams were riveted- and as the rivets were a potential weak point, there was quite a lot of attention paid to specifying how many, how big, and how and where they were placed. Once the main seam was riveted, a man with something like a cold chisel and hammer ( later , a pneumatic impact driver) would hammer along the edges of the overlapping plate, upsetting the metal there to create a tight seam. But running through any boiler made after around 1830 would also be a lot of tubes. They would either carry water ( water-tube boiler) or hot gas from the firebox ( fire-tube boiler). The ends of those tubes would be rolled over, to fit very tightly against the holes through the thicker plates, called crownplates, of the boiler. Those tubes would be the most susceptible to corrosion, and would be most likely to work loose with the expansion and contraction from heat. So, a very regular procedure would be to re-tube the boiler. There are any number of steam engine manuals from the 19th c. that describe it, but here is a pretty good video of a boiler getting new tubes. It gives you a much clearer view than a book could.
There are a lot of boilers still made this way, so re-tubing is by no means an obsolete process.