Ionic bonding occurs when an atom or molecule completely transfers electrons to another atom or molecule. This happens because the valence electron of what becomes the cation is attracted to what becomes the anion. This transfer of electrons causes electrostatic attraction; the receiving atom or molecule develops a negative charge to become an anion, and the transferring atom or molecule a positive charge to become a cation. The atoms and/or molecules, now having opposite charges, are attracted to each other, thus forming an ionic bond. Some examples of compounds with ionic bonds are NaCl (sodium chloride or table salt), and CaCO3 (calcium carbonate). Such compounds are called ionic compounds, as opposed to covalent or molecular compounds which have no ionic bonds at all.
The charge of a cation or anion is denoted as a superscript after the formula of the ion. A + sign denotes a positive charge resulting from loss of electrons. A - sign denotes a negative charge resulting from gain of electrons. The number of electrons missing or gained is written before the + or - sign to indicate the degree of charge; when a + or - sign is seen alone in the superscript, the charge 1+ or 1- respectively is implied. For example, the cation of NaCl is Na+, while the anion of CaCO3 is CaCO32-.
Typically, metals form the cation and nonmetals the anion, but there are exceptions. In ammonium chloride (NH4Cl), for example, the cation is NH4+, which is completely made of nonmentals.
When neutral single atoms of Na and Cl come together, the sodium atom may transfer a negatively-charged electron to the chlorine (see animation below). This leaves a positive charge on the sodium, and a negative on the chlorine, creating Na+ and Cl−. These charged atoms are called ions, and they will be attracted towards each other, forming an ion pair of sodium chloride. This attraction of oppositely-charged ions is called an ionic bond.
When a molecule picks up or loses one or more electrons, the result is a polyatomic ion. CO32- is a polyatomic ion consisting of a central carbon atom bonded to three oxygen atoms. Its overall charge is -2, having two more electrons than the total of what the carbon and oxygen atoms would normally have.
Two ionic compounds can switch their cations and anions in a double replacement or metathesis reaction. For example, solutions of barium nitrate and sodium sulfate can be mixed to precipitate solid barium sulfate: Ba(NO3)2+Na2SO4 → BaSO4+2NaNO3.
A free element can replace another element or polyatomic ion as the cation or anion of an ionic compound in a single replacement reaction:
In either case, ions that do not change in the formation of new ionic compounds are spectator ions; the Na+ ion in the latter example remains unchanged except being ionically bonded to a different anions.
Properties of ionic compounds 
Because of the strong binding of the crystal lattice of most ionic compounds, and well-packing, they tend to have very high boiling and melting points. Ionic compounds do not conduct electricity while dry, but when the ions are separated in solution, they conduct electricity well. Ionic compounds tend to dissolve in substances with polar covalent bonds, such as water. Most do not dissolve in non-polar compounds like (CH4). For what is meant by "polar" or "non-polar", see covalent bond.
Differing between ionic and covalent bonds 
Although ionic and covalent bonds are generally considered separately, there exists substantial grey zone between them. Beryllium chloride (BeCl2), which one might expect to be ionic, actually has significant covalent character.
In general, although metals tend to form ionic bonds with most nonmetals, the electronegativity differences between the bonding elements is what determines the degree of covalency of a bond. Electronegativity, for our purposes here, can be defined loosely as the affinity of the atom for the electron pair in a bond with another atom. Fluorine is the most electronegative element, assigned a dimensionless electronegativity value of 4.0. Caesium is the least electronegative non-radioactive element (francium may be less electronegative but is highly radioactive), with a value of .7. The greater the electronegativity difference, the greater likelihood of a covalent bond.
Likewise, there is only a certain degree in which metals, especially transition metals, can completely lose electrons. For example, ruthenium tetroxide (RuO4) is highly covalent, even though oxygen is much more electronegative than ruthenium, because even oxygen cannot completely take all 8 of the electrons ruthenium uses to bond. Lacking the electronegativity for this, oxygen must settle for double covalent bonds. On the other hand, ruthenium dioxide (RuO2) is, for the most part, ionic, as only 4 electrons have to be contributed to bonding by ruthenium.