San Francisco, where it's located

The climate of San Francisco can be defined as "Mediterranean with cool summers". In fact, both the winter temperatures and the rain pattern remind of the Mediterranean climate, but summer is unusually cool because of both the cold current that flows along the coast of California and the fog that forms in San Francisco Bay, which in summer functions as a natural air conditioner for the city and the surrounding areas.
Here are the average temperatures.
San Francisco - Average temperatures
Min (°C)567810111212121086
Max (°C)131516181921222223211814
Min (°F)414345465052545454504643
Max (°F)555961646670727273706457

However, in the area there are different microclimates, which must be taken into account by those who plan to move outside of the city proper (for example, to visit the Yosemite National Park). In general, as you move away from the sea, you find a more continental climate, a bit colder in winter and much warmer in summer.
Rainfall is not abundant, since it amounts to about 485 millimeters (19 inches) per year, but the period from November to March is quite rainy; in April and October, the rains are quite rare, while from June to September they are definitely rare and sporadic.
Here is the average precipitation.
San Francisco - Average precipitation

Winter, from December to mid-March, is mild and quite rainy. There are, however, sunny and dry periods, sometimes long. Very cold days are rare: at most the night temperature can approach freezing (0 °C or 32 °F), and only on colder periods it remains about 8/10 °C (46/50 °F) during the day. However, east of the city, in the Sacramento Valley, and more rarely also to the north and the south of the bay, there may be night frosts.
Every so often, fog can form in the city, while this phenomenon is more common in the Sacramento Valley.

Spring, from mid-March to May, is cool or slightly cold at night, while the days become gradually milder, and sometimes pleasantly warm, though with a moderate breeze in the afternoon that lowers the temperature. The rains become increasingly rarer.

Summer, from June to September, is cool, and it is usually foggy in the morning and sunny in the afternoon, with a cool breeze blowing from the sea. September, albeit slightly, is the warmest month. The foggiest districts (and also the windiest in the afternoon) are the westernmost ones (see Sunset District). The rains are rare, especially in July and August, when basically it never rains.
Daytime temperatures from June to September are around 17/19 °C (63/66 °F) in the towns on the Pacific coast (see Half Moon Bay) and in the western districts of San Francisco, around 21/23 °C (70/73 °F) in the eastern districts, 22/24 °C (72/75 °F) in Oakland and Richmond (on the other side of the bay), 25/26 °C (77/79 °F) in the towns on the south shore of the bay (see Palo Alto, Mountain View), and up to 28 °C (82 °F) in San Jose, just south of the bay.
Beware though: it's sufficient to move to the east, in the Sacramento Valley (see Sacramento, Stockton, Modesto), to find very high daytime temperatures, usually about 33/35 °C (91/95 °F), and sometimes even above 40 °C (104 °F).
In the following map, we can see the average maximum temperatures of August, which shows the significant differences that occur in the area. The city of San Francisco is indicated by a red star.

Average maximum temperatures in the San Francisco area in August (Celsius and Fahrenheit)

Every so often, however, hot air from the desert manages to reach the city, so much so that the temperature can reach 35/38 °C (95/100 °F) from May to October. Anyway, these periods of hot weather are very rare (in fact they may even not occur for a whole year), they do not last long (just one or two days), and the heat is felt only during the day, while in the evening the sea breeze brings back cool air. For example, in the 14th of June 2000, the temperature reached 40.6 °C (105.1 °F), but the minimum temperature of the previous night had been 13 °C (55.5 °F), and that of the next night was only 16 °C (61 °F).

Autumn, in October and November, is initially mild, and gradually becomes cooler and rainier. Usually, at night it begins to get a little cold in late October.

San Francisco

The amount of sunshine in San Francisco is sufficient in winter, when there is a moderate amount of sunny days, alternating however with cloudy periods, it's very good in spring, when sunny weather prevails, and it's still very good in summer, at least in the afternoon, while in the morning there are frequent fogs and mists.
San Francisco - Sunshine

Because of the cold current, the sea is cold all year round, and its temperature remains quite stable, since it ranges from 12 °C (54 °F) in the period from January to June, to 14 °C (57 °F) in the period from August to November.
Here are the average sea temperatures.
San Francisco - Sea temperature
Temp (°C)121212121212131414141413
Temp (°F)545454545454555757575755

Best Time

The best time to visit San Francisco is summer, from June to September, since it is the driest and warmest of the year. However, you have to take into account cool temperatures, and sometimes even a little cold, at night and in the early morning, and morning fogs, so it's better to bring a jacket and a sweater. Usually, in the months of April, May and October the weather is good as well, in fact it's often mild and sunny, even though there's a greater chance of rain; in addition, it can be cold at night, especially in April; moreover, in spring the afternoon breezes are quite intense.