Hello Jean, welcome to the boards!
Bagheria looks to be about 15 minutes away from Palermo and it sits on the Cefalu line so you'll pass by it on your way to Cefalu anyway. Searching the Trenitalia site the trains run more than hourly and take about 15 minutes to get there so it should be super easy to visit.
You can check options here: https://www.trenitalia.com/en.html
When you're there download the Trenitalia phone app it's super helpful.
Since it is so close I would suggest you could do it as a day trip from either Palermo or Cefalu - although that's further at 45 minutes or so. The advantage would be that you are not traveling with your baggage so you wouldn't need to worry about left luggage while exploring Bagheria.
Looks like a cute little town and is certainly close enough on the train to pop over and explore for a day.
Have a great trip!
=Tod