The train station of Caserta is located right in front of the Royal Palace, so it's a super convenient way to reach the palace. From Naples Central Station it will take about 45/50 minutes to reach it, costing only €4.
I always buy my tickets online through Trainline, so I don't have to deal with the queue at the ticket machines. I highly recommend this app, as it's superfast to purchase tickets.