Good morning and thank you for writing. Government programs can include mortgages like FHA, VA and USDA, down payment assistance (DPA), and mortgage credit certificates (MCC). So where the money comes from, and where it goes depends on the program.
Government-backed loans are in most cases provided by mortgage lenders, not the government. The USDA, FHA and VA oversee insurance programs which cover the lender if the borrower defaults and borrowers pay mortgage insurance premiums or funding fees. The money goes into an escrow account, and an escrow officer or attorney distributes the funds to sellers, real estate agents, etc.
For DPA, it's pretty much the same. If you are eligible (most likely determined by your income and where you buy), you may get funds for your down payment and / or closing costs in the form of a grant or a loan. Many times, the loan is interest-free and you only repay it when you sell. That money also goes into the escrow account, as will any money you contribute from your own funds.
MCCs, or mortgage credit certificates, subsidize mortgage interest for eligible buyers by providing a credit to you - cutting your tax bil and leaving you more money for a mortgage payment.
Good luck and thank you for your question.