Let's say player A is an unrestricted free agent (as most free agents we're talking about are)
His contract is up, he no longer has any ties with his now former team.
He has a choice between team A, B, or C.
So let's say he chooses team A.
He goes to his former team and says, "Let's do a sign and trade with team A. I can leave and sign with them outright, or you can be the ones to sign me to a contract, letting me get an extra year, and receive something as opposed to nothing in return for your troubles."
The salaries don't have to match as long as the former team is below the cap with the removal of the newly signed contract of player A.
Here's an article on Hoopsworld about sign and trades:
However, questions often arise as to whether or not this is the best course of action for a NBA team. Since the marquee free agent in question has more leverage than his current team in such negotiations, the player's current team risks being offered scraps or getting just pennies on the dollar in return.
Let's look at the cap situations of the free agent PFs teams
Amare Stoudamire - Removing Amare's salary, the Suns are at 45.6 million in salaries, similar to the Raptors.
David Lee - Do we even need to take a look at the salaries they have for next year?
So you can see from this that the salaries don't have to match for any of these guys, really. In theory we could give them Keyon Dooling and call it a day. The team trading Player A has no power over us or their player. His contract is up, he is no longer bound to that team. He is actually doing them a favor by letting them get something in return instead of leaving them with nothing.