There are different types of ways fog can form.
The first way is called Radiation Fog. Radiation Fog forms on a clear night with calm winds. You need the temperature to meet or come close to the dew point. The sun heats the earth during the day, but at night the energy escapes back into the air cooling it. When the air is cooling, it helps the air to reach the dew point creating the fog.
The second way is called advection fog. This fog can happen even if it is windy. This fog occurs when the ground is cool and a warm, moist wind moves over it. The air is cooled from the ground below helping it reach the dew point to create the fog. This fog you can have a cloudy sky and it will still form.
No matter how the fog forms it can make it difficult to see for driving and it is best to take precautions. When driving through foggy conditions, it is a good idea to use your low beams instead of high beams. High beams can reflect the light off the fog making it more difficult to see. Also, always allow yourself extra time to travel through the fog and stay a safe distance away from the car in front of you.