They basically wrote massive amounts of naked Credit Default Swaps (protection against default backed by nothing).
The demise of AIG could have created a domino effect that may have toppled the entire global financial system. The government really had no choice in that matter but to rescue them.
This is why credit default swaps should be regulated and backed by collatoral like insurance policies.