How can countries be in so much debt? You regularly hear in the media how countries are billions or even trillions of US dollars in debt. How does a country get so far in debt, and who is it in debt to? How can countries of the world operate owing such vast amounts of money?
How does a country get so far in debt, and who is it in debt to? 


Mostly because of government-issued bonds. These can be held by other governments, private entities or even the various government agencies: In the US, starting with the last category:


  Intragovernmental Holdings. This is the portion of the federal debt owed to 230 other federal agencies. Intragovernmental holdings total $5.6 trillion, almost 30 percent of the debt. Why would the government owe money to itself? Some agencies, like the Social Security Trust Fund, take in more revenue from taxes than they need. Rather than stick this cash under a giant mattress, these agencies buy U.S. Treasurys with it.
  
  Debt Held by the Public. The public holds the rest of the national debt of $14.7 trillion. Foreign governments and investors hold almost half of it.


Since I see you're from the UK... over there bonds are called gilts. And here's a breakdown of their ownership from 2009 (since that's what I could easily find):




  The biggest owners of gilts are insurance companies and pension funds. For them, gilts are predictable investments, which are particularly useful when a low-risk product is needed. Pension funds, for example, will usually switch an individual's holdings from higher risk investments, such as shares in companies, and put them in gilts as the person gets closer to retirement.
  
  Next on the list comes investors overseas. Unfortunately, nobody keeps records showing in which countries these gilts are held. This is in contrast to, for example, the US, which regularly publishes lists of the countries that own its Treasury Bills. So we know that at the end of 2009, $302.5bn (£196bn) of US government debt was held in the UK, making the UK the third-biggest investor behind Japan and China.
  
  The next biggest category holding UK government debt is banks, which includes the Bank of England. Since March 2009, as part of its quantitative easing programme, the Bank of England has bought just short of £200bn of gilts, making it the third-biggest holder of UK government debt.


And a more recent (2017) article (although lacking a pretty graph) roughly paints the same picture for UK's debt:


  Traditionally, around 40% of UK public debt was held by pension funds and insurance companies, which form a captive market because of their need for predictable and safe long-term investment returns. Around a quarter was held by other UK financial institutions and households, and the rest by overseas investors.
  
  QE [quantitative easing] has disrupted this pattern. Now, around a quarter of issued gilts are held by the Bank of England under its Asset Purchase Facility. Official data still show overseas holdings at around 27%, with insurance and pension funds’ share down to 28%, and other UK financial institutions holding around 40%. But once the store of gilts sequestered on the Bank of England’s balance sheet is excluded, overseas buyers account for 35% of gilt holdings and other central banks another 11%.
  
  Uniquely, more than 10% of the UK’s debt cannot be traced to either domestic or overseas buyers due to the paucity of data from the regular auctions. Lack of clarity on who’s holding the debt makes it harder to judge the risks of buyers selling-off or staying away. That fuzziness that may have helped to keep the markets calm.



How can countries of the world operate owing such vast amounts of money?


It depends on the country. Countries whose currency is attractive enough, e.g. the US dollar can "get away with it" by "printing" more money (see quantitative easing), although they can't do that too fast because that can cause inflation. The bonds of the big countries have had somewhat of "miraculous" negative real interest rate in the short run recently (although lacking a pretty graph):


  UK national debt is currently issued at a yield of less than 1% – far below the rate of inflation (2.6% in July). And this means Britain can effectively raise money free of charge in real terms. [...]
  
  So why are UK bonds still flying off the shelves, allowing the government to plug the gap in its spending plans with virtually “free” money? It is possible that investors are snapping-up new gilts simply through a lack of alternatives. If (as some fear) overpriced stock markets and property are about to crash and once-buoyant emerging markets are in trouble then the public debt of stable rich countries becomes the least worst option. US and German debt issues are proving similarly popular at rock-bottom yields.


Although this year the bond rates are expected to tighten, they are still mostly negative in real terms (for the big boys, but not so much for the smaller fish with weaker economies; the exception being the US which slighly above zero):




  Negative real borrowing costs add fuel to markets that are already on fire, limiting the downward pressure on bonds, keeping corporate spreads on the tight side and pushing stocks to new high after new high. [...]
  
  For investors, it’s a chicken and egg scenario. They’re crying out for policy “normalization” - higher rates and less central bank “interference” in markets - but are enjoying the asset price boom that’s in large part a direct consequence of central bank largesse.


Countries whose debt is denominated in a foreign currency, or a currency they can't print for some other reason, e.g. being in monetary union (ahem, Greece) can be in a world of hurt if they suddenly are threatened by default (inability to pay back on schedule). Typically the IMF steps in but it also imposes conditions in terms of structural reforms etc. for their bailouts.

E.g. news from July this year:


  Greece is scheduled to end nearly a decade of external help this August, after implementing more than 400 policy measures demanded by creditors. The yield on the 10-year government bond has fallen about 30 basis points in the wake of the recent debt deal. It moved from standing at about 4.2 percent to 3.9 percent. In contrast, Italy’s 10-year yield currently stands at 2.668 percent and the U.S.’ at 2.85 percent.