A few different ways, not the least of which is bond sales to citizens. When a government issues a bond, it is effectively putting out an IOU to the purchaser of that bond. Theoretically anyone or any entity (e.g. a foreign country, a foreign bank, etc.), depending on the countries regulations, can purchase these bonds. This is how countries can run up debt.
The issue Greece ran into, and the scenario you mentioned where countries cease to be able to run up debt, occurs when investors effectively lose confidence in the bonds issued by that country and refuse to buy them. The country must then offer higher interest rates to attract buyers or they simply cease to be able to finance their debt via this mechanism. This is what people are referring to when they say things like "the Chinese own the US" and other such things.
Another popular alternative countries have used in the past is to simply print more money, assuming they had control of the money supply. Obviously this is a method doomed to failure in the long run.