The most basic difference between these two substances, is that butter is derived from animal fat while margarine is derived from hydrogenated vegetable oils.
Butter is a much older food product than margarine. Butter is produced from the fatty cream of cow's milk, although other animal milks can be used. The fat molecules are usually held in suspension in the cream, but steady agitation in a butter churn causes the fat molecules to cling to themselves and eventually fall out of the cream as a thick mass of butter. Salt is often added to the raw butter, but is not strictly necessary. Churned butter is composed almost entirely of saturated fat, along with a significant amount of natural cholesterol.
Margarine, on the other hand, is a manufactured food product designed specifically to be a butter substitute in 1869. The early margarine products used beef fat as an ingredient, but most margarine formulas changed to vegetable oils by the early 20th century. The normally liquid vegetable oil is solidified through a process involving hydrogen gas bubbles passing through the mixture. The result is a solid butter substitute with no cholesterol and little if any saturated fat. Margarine does contain polyunsaturated and trans fatty acids, however, which many health experts consider to be unhealthy for humans because of their artery-clogging tendencies.
Butter and margarine differ in shelf life as well. Butter must be kept refrigerated in order to remain fresh for several days. Margarine should be refrigerated to maintain its solid form between uses, but it can remain stable much longer than butter. Butter, like many other dairy-based food products, can become spoiled or rancid without proper storage and refrigeration. Butter and margarine can both be used in recipes, but butter appears to be the default choice of professional chefs, while margarine is favored by more casual cooks. Butter is often more expensive than margarine, and the difference in flavor or texture between butter and margarine can be very subtle depending on the quality of the brand names used.