Specifically, I am taking the train from Taipei to Keelung, getting off at Badu and going to Pingxi, and then returning at night.
Do trains tend to sell out in Taiwan, or should I be OK purchasing my ticket immediately before departure?
Travel Stack Exchange is a question and answer site for road warriors and seasoned travelers. It only takes a minute to sign up.Sign up to join this community
Taipei to Keelung is effectively a suburban service, there are trains every 15-20 minutes and reservations are not required (or even possible for most trains?).
And even for long-distance trains, advance reservations are generally not necessary, unless you're traveling at peak season (eg. Chinese New Year) or on some special train (eg. the Alishan Mountain Railway).
If you are doing any amount of travel in Taiwan it's a good idea to pick up an EasyCard. You get discounted travel and you can also use them on trains, metro and many buses, plus they are valid in lots of shops like 7Eleven, great for picking up a snack without having to get money out.
For Pingxi you are better changing at Ruifang rather than Badu as you get better connections. There are a mix of local and various grades of express go there from Taipei Main Station (or Songshan) - locals you will be fine with the Easycard, express you will need to buy a ticket at the counter. If you're travelling at very busy times you might not be able to get on the next train but generally there is no problem.
Another option from Taipei is to get the 1062 bus from near Zhongxiao Fuxing Station to Ruifang. This also goes on to Juifen with is worth a visit.
Make sure you stop off at Houtong (many of the Pingxi trains start from here) as it's worth seeing the station cats.