A smart investor profits from discrepancies between price and true value. Basically, the margin of safety is that difference. Price is given by the market, value is partly determined by subjective factors, such as expectations about the future. Your valuation could turn out wrong. For example, earnings can go down, while you expected them to go up. The margin of safety is the amount of room there is for adverse developments, without your investment getting in trouble.
For bonds, one can say the margin of safety is the amount that the company earns in excess of interest payments, or the ratio between the total value and total debt of the company.
For stocks, the margin of safety is the extent to which "earning power" (or "earnings yield") exceeds the bond yield. Earning power is roughly the inverse of the P/E ratio, so earnings divided by price, and it is a measure of the amount that a company might be expected to earn if the current conditions remain the same. A small amount of the earning power is given to the shareholders as dividend. The rest is supposedly reinvested by the company, which may further increase both earning power and the value of the stock.
If P/E ratio = 20, earning power = 1/20 = 5%. Then, if the bond yield is 4%, the margin of safety is 25%, because earning power is 1.25 times the bond yield. This may seem large, but the earning power is really only 1% more than the bond yield, which is not much. Over a 10-year period that becomes a difference of 10%, but one should aim more for 50%. People investing in growth stocks use expected earnings for their margin of safety, which makes sense if estimate of expected earnings is a conservative. The problem with the typical growth stock is that the price paid is often very high.
The margin of safety varies for different investments, and is only part of what makes the portfolio a success. Diversification is another major aspect of risk reduction.
Obviously, margin of safety is not just about the value of the company, it is just as much about the price you pay. You may have to wait a while before the price of the stock you'd like to own comes down to a level at which there is a good margin of safety.