Margarine was invented in the 1860s by a French chemist as a cheap replacement for butter. Nowadays, margarine is frequently bought in the belief that it is a healthier option than butter. All margarine contains as much fat as butter, but some are lower in cholesterol and saturated fats. However the health benefits of many of these types of spreads has been called into question as most of them are made with hydrogenated (chemically hardened) vegetable oils and this process is believed to convert the polyunsaturated fat into trans-fats which have a negative effect on cholesterol and are now thought to be linked with heart disease even more than saturated fat.
Margarine is a highly processed food made by combining water and vegetable oils and usually containing emulsifiers, preservatives, additives, artificial colourings and flavourings and salt. There are many types available using different fats and with differing flavours and uses. Some are purely vegetable-based, containing no animal products at all, and are labelled dairy-free or vegan. Others contain a mixture of animal and vegetable fats.
Butter substitutes such as margarine and 'non-dairy spreads' vary in fat content, water content and flavour. 'Hard' margarine has the same fat content as butter, so is the best 'substitute' for butter - although your baked goods won't taste the same. Because it has the same fat content as butter, it isn't a low-fat option. Low-fat spreads cannot be used as butter substitutes in cooking.
Margarine can be used as a substitute for butter in most cases. However, some are designed for spreading, and others are hard and designed for baking so always read the packaging before cooking with margarine.