If you don't feel like walking up to the monastery and if you're taking the train from Madrid, take the bus that departs right outside the train station. It takes about 5 mins to get to monastery for EUR1.10.
Before you reach the bus stop at the town center, watch out for the monastery on your left hand side. From the bus stop you will have to walk for about 3 mins to get there.
I believe you can also take the bus back to the train station from the town center bus stop.
However, the train station is an easy walk from the monastery. It will take about 15 mins to go downhill.
You can do day trip from Madrid's Chamartin or Atocha station. The ride takes about 45 mins. After arriving El Escorial, you need to transfer a local bus to go uphill to the centre of the town EUR1.
From the bus terminal, you can walk to El Escorial by following a well lead sigh board.
On your return trip, you can take bus from El Escorial and the bus will drop you off one of the METRO station in Madrid.
By car is the best way to get around El Escorial, just because some of the attractions are far from the city centre and tourists buses won´t allow you to get on your way freely. If you´re in Madrid you will be able to rent a car very easily at any of the main train stations and/or the airport.