There are no direct trains between Madrid and Porto.
There is only one overnight train from Madrid to Coimbra, Portugal where you could change for Porto.
There are direct buses but these take about 8 to 9 hours. There are departures from Madrid Estacion Sur Coach Station by alsa.es as follows:
MADR-E.SUR PORTO 09:30 17:30
MADR-E.SUR PORTO 23:00 06:30 next day
You best bet might be by the air with a low cost airline like Ryanair.com
They have flights as follows:
Depart:Madrid 05:50 Arrive:Porto 06:10
Depart:Madrid 20:50 Arrive:Porto 21:10