Why can we finance government deficit by having a trade deficit? I encountered an example question regarding government deficit as follows:

$G-T=(S-I)-(X-M)$

where $G$ is government spending, $T$ is net tax (tax minus transfer payment), $S$ is total private sector saving, $I$ is investment, $X$ is export and $M$ is import.

Suppose deficit $G-T=\$84249$, $S-I=\$58558$, so $X-M=-25661$. 

The example question states that: to finance the deficit, private saving must exceed investment by $\$58558$, and to finance the rest of the government deficit, foreign imports ($M$) must exceed exports ($X$) by $\$25661$.

My question is, if there is a trade deficit (import is greater than export), that means the government buys imported products more than it sells the exported products; the government spends more than it earns, then how can it finance the government deficit?

This really confuses me. Thanks for any help!
To answer the question, I feel it is needed to correct one misunderstanding first: trade deficit does not mean the government buys imported products more than it sells the exported products. Rather, it means the imported products are more than exported products, which does not indicate whether the products are consumed or invested by the government or the private sector. 

And this question relates the double deficit problem (also called twin deficit).
Let's start from the no tax situation, and there is:
$$Y-C-G=I+NX$$ 
Since the LHS represents the total saving of the country (denoted as $S_{T}$), we have:
$$S_{T}-I=NX$$ 
Recall that saving is the surplus of a country's production that is not consumed by private and government.That's why total saving equals its investment ($S_{T}=I$) if it is a closed economy ($NX=0$).

In the perspective of an open economy, the consumption of a country's production is not only domestic spending on domestic products from the private sector ($C_{d}$) plus domestic spending on domestic products from government ($G_{d}$), but also foreign spending on domestic products ($X$). The private and government spending on foreign goods, as well as investment on foreign goods and services, should be eliminated from GDP or $Y$, and we have $M=C_{f}+G_{f}+I_{f}$ ($Y=C+G+I+X-M$)

Come back to the equation $S_{T}-I=NX$. The RHS is the trade balance, and the LHS can be view as the balance of this country. If the LHS is negative, it means the country has less saving than it has invested, which means it is the borrower in the global capital market (there is a capital inflow) in order to purchase foreign goods and services. This is caused (or accompanied) by a trade deficit, because the country produces ($X$) less than it consumes ($M$), which requires it to borrow money in the global market (if not, say, in the case of no capital flow, its currency will depreciate to balance $NX$, due to an excessive supply of domestic currency caused by trade deficit).  

Adding tax is simply separate total saving into private saving and tax, which is just slightly different from the original equation, that is,
$$S+(T-G)-I=NX$$
As government spending is always exogenous, and others factors are relatively stable, the double deficit hypothesis supposes that an increase in government deficit will result in an increase in trade deficit, accompanied by an increase in capital inflow to finance the extra government spending.