Piketty is correct, this is well known mechanism thats discussed in any international economics 101 textbook.
I think the best way how to see how this work is to forget about money for a second. Ultimately all economic transactions are not about money but about exchanges of real goods and services. Money are just facilitators of transactions, they solve the double coincidence of wants. For the purposes of this discussion you can think of money as just technology that makes transactions easier and go faster like internet which allows you to send letters faster and easier compared to post office, but what really matters is the content of a message.
So lets forget about the money for now and consider a barter economy where you have your country X that does not want anything from country Y but country Y wants coffee from country X. In such situation country X would either:
- export the coffee for nothing in return essentially making the coffee foreign aid - a noble act for sure but alas most people/countries will not just generously give away their products or services for nothing.
- the country X exports the coffee in exchange for country Y promising to pay them some goods and services back at some time in the future (i.e. for debt). Now debt is an asset for country X (for the creditor) and liability for country Y (borrower) so their net foreign assets position changes.
If you introduce money the same thing occurs except the debt is denominated in money terms rather than in some goods and services. In addition if we introduce not just money but also assume both country X and Y have their own separate currencies, dollars and pounds lets say then also some important exchange rate effects come in the play.
For example, if country X is the exporter country Y must always go through forex market to exchange their pounds for dollars to pay for X exports as people in X want dollars (since they dont want anything from Y and you can’t buy things in X with pounds). This will increase the demand for dollars at the same time as increasing supply of pounds. Hence the value of dollar rises (ceteris paribus) and value of pound decreases (ceteris paribus). This change of relative value of currency makes all foreign denominated assets and liabilities worth more so if the country is a net debtor currency depreciation increases its foreign currency debt burden making the net asset position even worse.
In addition this goes little bit beyond the scope of the question but eventually running trade account deficit leads to higher interest rates relative to those that run surpluses which encourages people to actually invest (i.e. purchase assets) in the country running trade deficit to the point where the flow of foreign investment is high enough to force exchange rate to equate the real returns in home and foreign country. An important caveat is that there are also other factors at play for example Batra & Belad (2013) show that thanks to USD being reserve currency US managed to keep low interest rates even though running huge trade deficit. This being said this is how, in general, the mechanism works in the long run and then some special circumstances can create exceptions.