The first soaps were probably the saps of certain plants, such as the Soap Plant (Chlorogalum pomeridianum), whose roots can be crushed in water to form a lather, and used as a shampoo.
Later, people learned that fats would react with alkalies in the ashes left over from a fire to produce saponified compounds such as sodium stearate and the related potassium stearate.
Today, soaps are made from fats and oils that react with lye (sodium hydroxide). Solid fats like coconut oil, palm oil, tallow (rendered beef fat), or lard (rendered pork fat), are used to form bars of soap that stay hard and resist dissolving in the water left in the soap dish.
Oils such as olive oil, soybean oil, or canola oil make softer soaps. Castile soap is any soap that is made primarily of olive oil, and is known for being mild and soft.
As warm liquid fats react with lye and begin to saponify, they start to thicken like pudding. At this point dyes and perfumes are often added. The hardening liquid is then poured into molds, where it continues to react, generating heat. After a day, the bars can be cut and wrapped, but the saponification process continues for a few weeks, until all of the lye has reacted with the oils.
Soaps are often superfatted, so after all of the lye has reacted with the fats, there are still fats left over. This is important for two reasons. First, the resulting soap is easier to cut, and feels smoother on the skin. Second, the extra fats make sure that all of the lye reacts, so no lye is left to irritate the skin, and the resulting soap is not too alkaline.