I read in a textbook that before WWI (it literally says "until 1914") USA was a debtor to European countries. However, at the end of the war, the European countries were the ones owing debt to USA because they needed fund for war. How is this possible? How could USA change the situation so quickly and become the borrower instead? What I am trying to understand is how is it possibe that USA was in debt if it had enough resources or funding to sustain a war and even lend money to other nations.
So there are two answers to this question, one economic and one historical: I'm not really qualified to discuss the economics of debt and how you can fund war while still being in debt (which is a thing that the US does all the time) and I'm not going to sit here and pretend that I know much about that process, so I'm going to do a quick overview of just the simple historical facts instead. The super basic answer to this question is that war is expensive and the money needs to come from somewhere. The United States essentially bankrolled the entire war effort for several European countries (the UK, France, Italy, Russia, and Belgium in particular); by the end of the war, various European countries owed their creditors in the US over $9.9 billion. However, it wasn't actually the US government that funded this effort (who as far as we know did not actually lend that much money comparatively speaking) but private US banks like JP Morgan. Since the government wasn't actually directly involved in the financial process, this not only solves the problem you posed in your question about how the US could be in debt but still have the resources to fund/sustain the war effort, but also led to a problem for European countries: for-profit private banks have zero desire to wipe your debt for political points. They just want the money back, plus interest.
This was essentially the problem that European countries faced that formed the basis for the idea of German reparations. Besides the fundamental idea of "we need to punish Germany for starting the war," the basic idea of reparations was that because all of the other countries were heavily in debt, they needed Germany’s money (especially since they were one of the few participating countries to come out of the war with their economy relatively intact) to pay the US banks so that they didn’t have to tax their own people. Otherwise, the European countries felt, they were the ones getting screwed: they won the war, but they were the ones paying for it while Germany got off (in their minds) relatively "scott free."
This then led to a sort of circular financial issue, because the German reparations numbers were absolutely ludicrous, but when Germany demanded a cut in reparations, the UK and France said, "we can’t cut the reparations bill down unless the US cuts our debt." The US government under Wilson essentially shrugged their shoulders and went "Well you didn't borrow from us; we can't do anything about it," and the private US banks said "We lent it to you fair and square; now you have to pay it back," because private banks are in the business of lending money for profit; otherwise they wouldn't do it. And then the British and French would go back to the Germans and go "sorry, we can't cut your bill" and the cycle would begin all over again. Obviously this is a super simplified explanation, but I hope this gives you a general idea of how the US could have bankrolled the war effort while still being in debt.
There are several solid books exploring this issue if you want to learn more about the topic: