I have never drove from SF to Yosemite but I would think it will take at least four hrs one way to get to the Yosemite Valley. Depend what you want to do in the park, you can decide whether over-night stay is needed or not.
Accommondation runs from Wawona Hotel (high-end) to Curry Village's canvas tent cabin (inexpensive but with public and shower). If you decide to stay at a tent cabin, make sure you have a flashlight. There is no light between your cabin and the public restroom.
www.yosemitepark.com/Accommodations.aspx