Pearls are formed when an irritant, such as a bit of food, grain of sand, bacteria or even a piece of the mollusk's mantle becomes trapped in the mollusk. To protect itself, the mollusk secretes the substances aragonite (a mineral) and conchiolin (a protein) in layers, which results in a pearl.
Depending on how the aragonite is arranged, the pearl may have a high luster (nacre, or mother-of-pearl) or a more porcelain-like surface. Pearls may be a variety of colors, including white, pink and black.
Any mollusk can form a pearl, although they are more common in some animals than in others. There are animals known as pearl oysters, which includes species in the genus Pinctada. The species Pinctada maxima (called the gold-lipped pearl oyster or silver-lipped pearl oyster) lives in the Indian Ocean and the Pacific from Japan to Australia and produce pearls known as South Sea Pearls. Other pearl-producing animals include abalones, conchs, pen shells,and whelks. Pearls may also be found and cultured in freshwater mollusks and are often produced by species collectively called "pearl mussels."
Some pearls are cultured - they do not form by chance in the wild, but are helped by humans, who insert a piece of shell, glass or mantle into a mollusk and wait for pearls to form. You can read more about this process here.