As njuffa alludes to there is a current just offshore from the California coast. This is a deep, cold ocean current. As it hits the continental shelf off the coast it is deflected upwards towards the ocean's surface. But here's where it gets different: a thermal low that forms in the Central Valley of California during the summer draws in the air from the Pacific through the delta and bay waters. As that air meets the cold upwelling water that was deflected upward by the continental shelf, it draws moisture from it. That cold water also cools the air which then forms fog and gets drawn into San Francisco. As that moist air gets drawn further inland, it warms sufficiently for the fog to return to vapor. This actually cools the Central Valley to where this onshore breeze (we call it the delta breeze) gets shut off, the valley warms again, and the whole cycle repeats. Of course, other weather factors (position of the Eastern Pacific High, for instance) can override this process, but it is pretty much the standard summertime pattern.
If you need citations, I'm not sure I can provide them, as this is something I learned in a physical geography class in college many years ago. I've also seen some other explanations that are just plain wrong.