How do metals, nonmetals, and metalloids gain stability? What ions do metals and nonmetals form?