Explain why ionic compounds are formed when a metal bonds with a nonmetal but covalent compounds are formed when two nonmetals bond.