Madrid has two train stations for intercity routes, Atocha and Chamartin. Chamartin is closer to the airport.
It’s a difficult to answer this in the abstract. Barajas airport is massive. If your plane lands at one end of T4S, you could be walking a really long distance. I don’t know how fast you walk.
My experience is that Barajas isn’t crowded at this time of day and passport control is quick. I wouldn’t expect to wait more than 5 minutes.
Flights are also often delayed. I would build in some allowance for your flight to Spain being delayed.
The other thing is, what’s your tolerance for a mishap, or buying flexible tickets that can be rebook if you miss your train? And, how urgently do you need to be in another city?
Maybe you can book a later train with a lot of margin for error and then plan to get tapas in Madrid before your train.
In conclusion, use your own personal common sense.