Both retail and institutional investors have borrowed a record $642 billion to buy securities on margin only to see their portfolios take a hit when the Dow dropped more than 1,000 points recently.Buying on margin, essentially buying stocks or other securities with borrowed money, can be a way to magnify earnings and boost your portfolio big time. It can also, as many are learning, be a way to lose lots of money real fast.Related: HOW DELTA NEUTRAL STRATEGY WORKSHow It WorksBuying on margin refers to the down payment (margin) you give the broker with balance of the cost of the asset being purchased paid for through a loan. When you buy on margin, as with most loans, you must put up collateral. In most cases this is through what is known as marginable securities in your account. Before you buy on margin you need to open a margin account with the broker.Based on your creditworthiness and other factors, the broker will set a minimum margin and a maintenance margin that must be in the account before you can begin buying on margin. If the value of your portfolio falls below a set amount you may receive a margin call in which the broker instructs you to bring your balance back up to the required maintenance level. You can do this with cash or by selling some of your securities.Profit For BrokeragesProviding margin accounts is lucrative for brokerages. That’s because they charge interest for the money used to provide your loan. In addition, if you open a margin account with a broker chances are you will stay with that broker long-term because of your relationship.Margin debt has been rising for the past several years and is considered a gauge of investor confidence. The nearly decadelong rally in the stock market has encouraged even more margin debt. In the event of a market downturn, this margin debt could create some real problems for investors.The Roll Of The FedThe maximum value of a margin loan is set by the Federal Reserve Board and is a reflection of the value of the underlying securities being bought on margin. Individual brokerages are free implement more stringent borrowing policies, but they cannot operate at a lower level that that imposed by Federal Reserve.As noted above, the part of the trade not paid for by the margin loan can be paid in cash or by posting other securities as collateral. Normally those securities must be at least twice the value of any cash that would have been required. This provides the brokerage (and you) some breathing room since stocks tend to go up and down on an ongoing basis.Related: WHY STOCK PRICES CHANGEUpside And DownsideThe upside potential is obvious. If you can buy a lot of stocks on margin, any increase in the value of those stocks is magnified. For example, if you pay $10,000 for a stock and it doubles in value, you have a 100% gain. If you buy the same stock for 50% down ($5,000) and 50% borrowed (margin) funds, your gain would be 200% (after paying off the $5,000 margin loan).On the risk side, suppose you bought the securities for $10,000 and the value fell to $5,000. Your loss if you paid cash would be 50%. If you bought those stocks with 50% your own money and 50% borrowed funds, you would suffer a 100% loss. First, you would lose the $5,000 of your own money, leaving you with the $5,000 the stock was now worth. That would go to the broker to pay back the loan, leaving you with nothing.