How Fog Forms
Like clouds, fog forms when water vapor condenses into small droplets in the air. It can condense around dust or salt particles in the air, known as "nuclei." Fog largely depends on the proximity of air temperature to dew point, occurring when the two are within 4 degrees of one another. Fog forms when a cool air mass is trapped beneath a warm air mass, so it is much more common when a warm front moves into an area.
Frontal Fog
The majority of fog association with fronts comes from warm fronts. When a warm front moves in, it traps a mass of cold air beneath it, with raindrops continuing to evaporate beneath the warm air and vaporize. A layer of clouds begins to form beneath the warm air, eventually coming to the ground as fog. These frontal fogs are much more common than cold-front fonts, which are faster moving and as a result, have only brief episodes of precipitation.
Sea Fog
Sea fog is extremely common near oceans because of the salt content of the water: because salt can act as a nucleus, fog forms around small particles found in sea spray, generated by the motion of waves breaking against the coastline. Near the ocean, fog can occur in areas of lower relative humidity, so in certain areas, such as the California coast, sea fog tends to be more common than fog further inland.
Nonfrontal Fogs
Much of the fog that forms has little to do with fronts, including "upslope fog" and "valley fog." While these still rely on atmospheric conditions to form, they depend much more on geography than frontal fog: "upslope fog" is formed by wind moving moist air uphill, while "valley fog" occurs when cool air settles into a mountain valley as warm air passes over. Depending on your location, these fogs may be much more common than frontal fog.