I've been on a LONG conference call that gave me some time to research this. I made a possible route
(A)
Madrid to El Escorial 08:32 09:43
http://horarios.renfe.es/hir/hjhir030.jsp?&O=MADRI&D=10203&MON='EURO'&ID=s
(B)
El Escorial to Villalba de Guadarrama (back towards Madrid) 16:00 16:12
http://horarios.renfe.es/hir/hjhir030.jsp?&O=MADRI&D=10203&MON='EURO'&ID=s
(C)
Villalba de Guadarrama->Segovia 16:57 17:57
(D)
Segovia->Madrid 20:55 22:49 (last train back)
So, it's possible, but notice how much time you get to spend in Segovia: less than 3 hours...and taking the last bus back instead would buy you only another half-hour in Segovia.
Consider this:
1. Take the 08:32 train to El Escorial as shown above.
2 Instead of taking the 16:00 train to Villalba, take a taxi from El Escorial to the Villalba station in time to catch the 12:57 or 14:57. (If it takes only 12 minutes to get there by train, I can't imagine that the taxi ride could cost very much.) This would give you three or one hours more in Segovia, respectively (but of course, less time at El Escorial).
Bottom line: if you are relying on public transportation, you CAN do it, but the scheduling involved can make it a hassle.
"An honest man is always a child" - Socrates ...no wonder I'm so immature!