There are both trains and buses. Either option takes around 4 hours 40 minutes. I have just finished booking all our buses and trains for Spain and used the Trainline app as it let me compare buses and trains over the same routes.
The main train company is Renfe.com and the main long distance bus company is Alsa.com. You can easily book directly with them. For our trip I booked buses directly with Alsa, but booked my trains using Trainline as on some of our routes there are competitors to Renfe and Trainline showed me all my options.
If you book your trains well in advance you will get a significant discount. Buses seem to be the same price regardless of when you book.
You could also fly if you want to.