Anodizing
Anodizing is a chemical process akin to rusting, in that it works through electrically building up an oxide layer on a metal. When light hits the metal, part of the light bounces off the outside of this layer, and part bounces off of the inside. Depending on the thickness of the oxide layer, different wavelengths of light are "cancelled out" and the metal appears to be a different color.
Many metals can be colored through the process of anodization, but niobium and titanium are the only two commonly anodized metals that are used in the body modification industry. The range of colors available is dictated by the different thicknesses of oxide layer that can be built up, and not all colors are available.
It should be noted that black niobium or titanium is actually achieved through a thick firescaled oxide and not through the process described above. It should also be noted that "anodized aluminum" and other metals may not actually be anodized as described here, but may instead be "glazed" with a coloring agent.