A detergent is a
synthetic combination of a
surfactant and constituents (don't worry, there will be more detail on these below) which allow it to work better in its context. It differs from a
soap in that it is considered synthetic, where a soap would be made from natural ingredients. Because detergents are synthetic, they can be designed to leave less residue and combine in specific ways with specific kinds of dirt. Given these advantages, detergents are used for many more cleaning and
emulsification needs than soaps, and have been in at this level of use for the past fifty years.
The first detergent was synthesized in Germany during World War I when animal fats were too scarce to be used for soap. Production began in the United States in the early 1930's, but did not catch on until World War II caused a fat shortage there as well. Around this time it was discovered that detergents could be formulated in such a way as to not combine with minerals in water, and thus not leave behind "soap scum" on clothing and containers. Research during WWII even led to a detergent which would remove soils even using frigid sea water, a temperature and mineral range that would have made soap worthless. By 1953, sales of detergent in the US had surpassed sales of soap, and today new detergents are made for niches where only soap has been usable before.
Most of the real work done by a detergent revolves around a kind of chemical known as a surfactant. Having water bead up on a surface is great when one is trying to stay dry beneath it, but not so good when the surface is dirt that needs to be washed away. Surfactant interrupt water's surface tension, so it is no longer great enough to keep a hydrophobic (literally, water-fearing) surface from getting wet. These chemicals are able to do so because their molecules have two sides: one which can bond to the hydrophobic soils, and the other which is hydrophilic (water-loving) and easily bonded by water. Taken together, the surfactant allows the water to distribute over the surface, attaches to the soils to be removed, and keeps them emulsified in the water until it is rinsed away.
For all purposes of chemistry, detergents synthesized from animal fats and hydrocarbon oils can be considered to work the same way. To derive a surfactant from hydrocarbons, it is first necessary to attach a hydrocarbon chain to a hydrophilic chemical, resulting in something akin to a synthetic fatty acid. In the equivalent step of soap-making, the animal fats already contain fatty acids in the form of triglycerides. Once these acids are isolated, they are combined with a soluble salt from an alkali metal such as sodium or potassium; this step combines the basic alkali with the synthetic acid to form a neutral detergent. It's probably easiest to think of the resultant molecule as having a hydrophilic alkali end, which is connected to an acidic section, and terminates in a hydrophobic hydrocarbon chain.
Most of the additives combined with detergents are known as "builders," and are used to enhance the detergent's action while being less expensive to produce than the detergent itself. Specialized phosphates are common builders, which bind to and sequester metallic ions (of calcium and magnesium in particular) in hard water, softening the water and making the detergent's job easier. Sodium carbonates, such as soda ash and sodium bicarbonate/baking soda, can be added to increase the effective alkalinity of the detergent, and thus make rinsing it away easier. When a color-safe bleaching action is desired, compounds such as sodium perborate and sodium percarbonate are used which release oxygen and thus act as a hydrogen peroxide bleach.
Another additive that's used very commonly in powdered detergents is sodium sulfate, which is used to dilute them and keep the powder particles from sticking together. While this chemical actually lessens the detergency of the combination, this disadvantage can be overcome with builders, or completely counteracted by the addition of carboxymethylcellulose (CMC).