A neighbour is trying to sell me some of his leftover styrofoam insulation. I need to insulate my attic. Is styrofoam a good insulator, or should I use another type of insulation?
That styrofoam insulation will work, but it is not cheap per R-value (however it gives you a great R-value per inch). Insulating an attic usually means you have a lot of room for insulation and you will need high R-value so that is why the cheaper options are more popular (blown cellulose or blown fiberglass).
Said another way, if you have a couple of feet of space in your attic, you can just fill that all up with loose insulation and get R-45 (or whatever you need). But if you have a smaller space, say it is a finished room and the only space for insulation is between the 2x6 or 2x8s ceiling joists you may want to pay extra to get a higher R-value in that limited space.
There are two types of polystyrene (Styrofoam) insulation: expanded and extruded. The extruded type prevents the movement of moisture while the expanded type lets water through. The extruded type has better insulating properties (R-5/inch) versus expanded (R-4/inch). Both of these are going to give you more insulating ability per inch than a batt or loose-fill. However, they are likely more expensive per "R" than loose-fill insulation, and will be more difficult to install (since you'll need to seal their edges to either your rafters/studs or to each other.
You need to be very careful about how you engineer a vapor barrier into your walls/attic. You should only have one vapor barrier in your wall so that water cannot be trapped, causing mold to grow.
There are two different ways to insulate attics: vented and unvented. For vented, you ensure that air can move between your eves and the ridge or gables of your roof. You then put insulation at the floor of the attic. For most of the United States, you'll optionaly want to first put down a vapor barrier (such as plastic, kraft paper, or extruded polystyrene) between the ceiling joists, and then put a lot of vapor permeable insulation on top.
For an unvented roof, it's much more complicated. You first install a very large amount of non-permeable foam insulation it the rafters (above your head, in region 5, R-20 of insulation in order to prevent condensation), seal it with spray foam, and then attach an ignition barrier such as rock wool or drywall beneath the foam. And then, add enough vapor-permeable insulation to up the total R value to the required amount. This way, you don't need to provide any ventilation for that attic, and its temperature will be around the same temperature of your living space.
In summary, any foam board has a "good" R value per inch, but a "bad" R value per dollar, and with foam insulation it is tricky to install it in such a way that will not cause either moisture problems or cause your shingles to heat up and fail.
I'd opt for a different type of insulation. The styrofoam insulation that Home Depot carries is only rated for R-5 per inch. That means even a 2" thick piece will only give you R-10 for $0.96/sq. ft. I just insulated my attic this weekend with blown-in cellulose insulation, and achieved between R-30 to R-45 for about $0.42/sq. ft. (Note: I used 20 bags, which is the minimum purchase for a free 24 hour blower rental at both HD and Lowes.)
Be warned though, it is the messiest thing I've ever done. Be sure to wear a respirator with the appropriate filters. Don't skimp and use a cheap disposable dust mask. At one point it was so dusty in the attic that I couldn't see more than 10 ft. in front of me.
You could also go with the blown-in fiberglass insulation option, which is less dusty, but more expensive.
Or, as @B Mitch points out, you could go with R-30 fiberglass batts for $0.83/sq. ft.