Spray foam insulation is a liquid that contains a polymer, such as polyurethane, and a foaming agent. When applied, it expands and hardens into a solid form, filling all available space.
Fiberglass insulation is made by melting sand and recycled glass together. It usually comes in a blanket (long roll) or batts (pre-cut rectangles), though loose fiberglass can be blown into a cavity.
Both can be made to give you the same R-value, but fiberglass will require a much thicker layer than spray foam. R-value stands for resistance to heat flow and is the way insulation is measured--the higher the R-value, the better the insulation.
Spray foam insulation always needs to be installed by a professional. Fiberglass batts can be installed by a do-it-yourself homeowner. Fiberglass insulation requires a separate air barrier, but spray foam insulation acts as its own air barrier. Fiberglass can leave gaps around obstructions such as pipes, but spray foam completely fills all crevices. Fiberglass insulation can deteriorate over time. Spray foam insulation never sags or degrades.
One of the biggest things to consider when deciding between spray foam and fiberglass insulation is your budget. Spray foam is significantly more expensive up front. You'll recoup your initial investment over time with big savings on heating and cooling costs, but that will take years.
Related to that, you should consider the climate where you live. In extreme temperatures, you'll see a bigger energy difference with spray foam insulation than you will in a more mild climate.
The benefits of spray foam insulation are numerous. It provides a tighter seal in hard to insulate areas, it lasts well over time, it reduces construction time and it makes for lower energy bills.
The major benefit of fiberglass insulation is its lower price. It's also possible to install it yourself, making it possible to further lower the cost. Fiberglass insulation is noncombustible, which is not true of spray foam insulation.
No matter which type of insulation you choose, be sure to install as high an R-value rating as you can. A little extra insulation won't add much to the overall cost of the project now, but trying to add more insulation later will be expensive and difficult. You'll be surprised what a difference a higher R-value rating can make in your energy costs.