The most common way to get from San Francisco to Stanford is to take the Caltrain from San Francisco Station (@700 4th St, intersects with King St) to Palo Alto Station, which takes 45-65 mins depending on the type of service (bullet/limited/full).
Downtown Palo Alto is a nice area to take a quick walk around if you haven’t seen it before.
To get to Stanford’s campus, you can either walk ~25 mins or take Stanford’s Marguerite Shuttle (weekend service limited to every 60 mins via the SE Shopping Express).
And finally: Stanford is huge and a walking tour can take anywhere from 1-5 hours based on what you want to see.
All of the above came from a local. I copied it into a blog post (a) so I can find it if and when I need it and (b) as a spur to your own explorations, should you find yourself in San Francisco with some time to kill.