They form when you get humid air that rises for whatever reason, condenses and forms clouds, and dumps precipitation and has strong enough updrafts and downdrafts to produce lightning. The really nasty ones form when you get a bunch of warm moist air on the ground, and then cool dry air slides in on top (this particular event is called a dry line).

This makes the atmosphere incredibly unstable since the warm air wants to go up really quickly. As it goes up and forms clouds, it creates an updraft that has to suck in more warm moist air from the ground to replace it. So the storm now feeds itself and continues to grow until it tops out at 30,000 feet and gets pushed around by the upper-level winds; which blow the top of the storm out in front of it to produce the classic anvil shape. Eventually the droplets of moisture get too big for the updrafts to hold them so they fall, creating downdrafts. Eventually the downdrafts tear the storm apart, but often not before it dumps baseball-sized hail and produces tornados if you're unlucky.