The best way to get to Puglia by train from Sorrento is to return to Napoli Centrale on the Circumvesuviana and then take a local train to Caserta to connect with a high-speed or Intercity train to Bari. The high-speed trains from Rome to Bari bypass Naples, so you’ll need to catch it in Caserta. I did the Sorrento to Bari trip a few years ago and it worked great. To get from Bari to Alberobello, you would then change to FSE, a local train company. There are numerous departures throughout the day, with the last train departing for Alberobello at 20:36. You can check schedules at www.fseonline.it. Not sure whether Bari Centrale, where the high-speed and Intercity trains arrive, and Bari Centro, where the FSE trains depart, are the same station. If not, I suspect they’re close to one another. Maybe someone else will know.
Keep in mind that the train trip from Sorrento to Alberobello is a long one and that there’s usually a big gap in the Caserta to Bari schedule midday, so plan to travel either early or in the afternoon. I chose to spend the morning in Sorrento and travel in the afternoon, but the morning options are faster if you don’t mind getting an early start.