There are many forms of spray foam. The two basic categories are “One-part” that requires moisture in the air to cure and “Two-part” that cures when the two materials are combined. One-part foam, commonly call “foam in a can” is usually for small scale applications such as sealing gaps and cracks. You can buy it at local building supply stores (Home Depot, Lowes, etc) or you can get larger cans and buy special guns to spray it.
For large to mid scale applications, a two component mixture comes together at the tip of a gun, and forms an expanding foam that is sprayed onto concrete slabs, into wall cavities of an unfinished wall, against the interior side of sheathing, or through holes drilled in sheathing or drywall into the wall cavity of a finished wall.

Blocks airflow by expanding and sealing off leaks, gaps and penetrations.
Can serve as a vapor barrier with a better permeability rating than plastic sheeting vapor barriers and consequently reduce the build up of moisture, which can cause mold growth.
Can fill wall cavities in finished walls without tearing the walls apart (as required with batts).
Works well in tight spaces (like loose-fill, but superior).
Provides acoustical insulation (like loose-fill, but superior).
Expands while curing, filling bypasses, and providing excellent resistance to air infiltration (unlike batts and blankets, which can leave bypasses and air pockets, and superior to some types of loose-fill. Wet-spray cellulose is comparable.).
Increases structural stability (unlike loose-fill, similar to wet-spray cellulose).
Can be used in places where loose-fill cannot, such as between joists and rafters. When used between rafters, the spray foam can cover up the nails protruding from the underside of the sheathing, protecting your head.
Can be applied in small quantities.
Cementitious foam is fireproof.

Advantages of closed-cell over open-cell foams
Open-cell foam is porous, allowing water vapor and liquid water to penetrate the insulation. Closed-cell foam is non-porous, and not moisture-penetrable, thereby effectively forming a vapor barrier. (N.B.: Vapor barriers are usually required by the Building Codes, regardless of the type of insulation used. Check with the local authorities to find out the requirements for your area.)
Closed-cell foams are superior insulators. While open-cell foams typically have R-values of 3 to 4 per inch (RSI-0.53 to RSI-0.70 per inch), closed-cell foams can attain R-values of 5 to 8 per inch (RSI-0.88 to RSI-1.41 per inch). This is important if space is limited, because it allows a thinner layer of insulation to be used. For example, a 1-inch layer of closed-cell foam provides about the same insulation factor as 2 inches of open-cell foam.
Closed-cell foam is very strong, and structurally reinforces the insulated surface. By contrast, open-cell foam is soft when cured, with little structural strength.
Open-cell foam requires trimming after installation, and disposal of the waste material. Unlike open-cell foam, closed-cell foam rarely requires any trimming, with little or no waste.

Icynene spray formula
R-3.6 (RSI-0.63) per inch. Icynene (polyicynene) "does not shrink, sag or settle." Icynene uses water for its spray application instead of any ozone depleting chemicals. Flammability is relatively low. Disadvantages: Expensive. Does not achieve code in all jurisdictions. Smoke is toxic.

is a plastic (open cell polyurethane foam) and therefore made from petrochemicals. Contact with skin, eyes, or respiratory system is hazardous during application. Similar hazards occur during manufacture. Isocyanates are the leading cause of workplace-related asthma and pulmonary disorders in many post-industrial countries.

Sealection 500 spray foam
R-3.8 (RSI-0.67) per inch. So called "water-blown" as it uses water in a chemical reaction to create carbon dioxide and steam which expands the foam. Flame spread is 21 and smoke developed is 217 which makes it a Class I material (best fire rating). Disadvantages: Is an Isocyanate.

"SoyTherm 50" spray foam
R-4.1 (RSI- 0.72) per inch. Made of renewable soy beans and yields the highest true R-value per inch of any open-celled "half pound" spray foam insulation. It contains no Volatile Organic Compounds (VOC's), asbestos, or formaldehyde and there are no CO2 emissions during the production process. It is the only soy based spray foam to be class I fire rated. Disadvantages: Historically the cost of spray foam has been high, but it is rapidly becoming more economical for homeowners to retrofit their houses.

Cementitious foam
Such as Air-Krete. R-3.9 (RSI-0.69) per inch. Non-hazardous. Is the only foam not restricted to a depth of application. Fireproof, will not smoke at all upon direct contact with flame and is a two-hour firewall at a 3.5 inch (or normal 2 × 4 in stud wall) application per ASTM E-814 testing (UL 1479). Great for sound deadening; does not echo like other foams. Environmentally friendly. Non-expansive (good for existing homes where interior sheathing is in place). Fully sustainable: Consists of magnesium oxide cement and air, made from magnesium oxide extracted from seawater. Blown with air (no CFCs, HCFCs or other harmful blowing agents). Nontoxic, even during application. Does not shrink or settle. Zero VOC emission. Chemically inert (no known symptoms of exposure per MSDS). Insect resistant. Mold Proof. Insoluble in water. Disadvantages: Fragile at the low densities needed to achieve the quoted R value [6] and, like all foams, it is more expensive than conventional fiber insulations.

typically R-5.6 (RSI-0.99) or slightly better after stabilization; higher values (R-7, i.e., RSI-1.23, or better) in stabilized boards.[6] Less flammable than polyurethane.

uses air as blowing agent. Shrinks while curing.

Closed-cell polyurethane
white or yellow (or purple in the UK). May use a variety of blowing agents. Resistant to water wicking and water vapor. Benefits of air tightness. BASF Polyurethanes U.K. Ltd. manufacture a spray foam insulation for new build and refurbishment specifications under the brand name WALLTITE.

Open-cell (low density) polyurethane
white or yellow. Expands to fill and seal cavity, but expands slowly, preventing damage to the wall. Resistant to water wicking, but permeable to water vapor. Fire resistant.

Great Stuff is a Dow Chemical product that comes in cans and consists of several complex chemicals mixed together (isocyanates, ether, polyol). Dow manufactures this for small applications, but there is nothing stopping you from buying dozens of cans for a large retrofit task, such as sealing the sill plate. Since the blowing agent is a flammable gas, using large quantities in a short time requires strict attention to ventilation. Toxic vapors are minimal due to low vapor pressure and what little there is should be removed quickly if adequate ventilation is used. However, a respirator with an organic vapor sorbent may be advisable in some cases, for example if the foam is heated Very thick applications should be done layer-by-layer to ensure proper curing in a reasonable time frame.

Honeywell's Enovate Foam Blowing Agent
is an HFC used in some closed-cell spray foam insulations. Although it has zero ozone depletion potential, it has a high global warming potential of 950 (meaning it is 950 times as potent as CO2 in its global warming effect). For example, E:zero spray foam solutions offers both open and closed cell varieties of spray foam insulation, some of which use Enovate high global warming potential blowing agents.