Governments work different than normal consumers. Normal consumers take their earning into account and then prepare their expenses accordingly. But governments take their expenses into account first and then decide how to finance them. If the expenses can be met by the income, fine. But the income does not meet the expenses, governments use different type of financing tools to finance their expenses which includes loans etc.