In case there are others out there curious about this I’ll toss out a quick answer. The simple explanation is that the remaining contract moves with the player but money already paid remains with the team, so things like signing bonuses already paid out but divided through the length of the contract are still on the cap for the team who paid it. The current and future salary moves with the player in most cases.
I said most cases, because there are times when a team wants a player but doesn’t have the cap space for them so the original team can pay some of that usually in return for a better trade value. We did this when we traded for Leonard Williams. The Giants reworked his contract to convert most of his remaining 2023 salary into a bonus to keep it on their books and in return we gave them a 2nd round pick which under normal circumstances would have been a little high for renting a player.