Polyimide aerogels, carbon aerogels, and metal carbide aerogels and methods of making same