The NBA salary cap is what's known as a soft cap. It limits the amount of money that teams can spend, but teams can exceed it if they are willing to concede certain penalties (usually monetary).
Some things that are worth mentioning here, with the lockout a few years ago, the NBA's salary cap structure changed. The two moves that you seem to be interested in (Dallas' dismantal and the Heat's collection) both happened under the old cap structure.
Salary caps are very complicated beasts (NBA wikipedia article here), but at their heart they say this:
- A specific portion of the league's revenue is set aside for player salaries, we'll divide that total by the number of teams and set that as the salary cap (with some wiggle room, probably down for teams going over). Usually there is also a salary floor which says that teams have to spend a certain amount of money on player salaries.
The NBA has additionally capped individual player salaries, a max contract player can only make a certain percent of the total salary cap available to the team.
You've asked a lot of questions here, but the answers to all of them are roughly the same:
Some owners are willing to pay the taxes on going over the salary cap. This is the case with the Heat, the Clips and the Nets (the Nets especially have huge luxury tax bills).
Some players are willing to take less than max deals. With the Heat, Lebron, Wade and Bosh all took slightly less than max deals to give the team flexibility, and you may see them all restructure in the coming year to give even more flexibilty to go get another player.
Each team has certain exceptions to the cap. There is an exception called the "Mid level exception" which allows a team that has used its entire salary cap to sign players for more than the minimum, this is how the Heat signed Ray Allen. There is also a veterans minimum that allows a team to sign as many players as they can roster for the minimum salary for their experience level regardless of the salary cap.
To address the Maverick's situation specifically, they had an aging team that was not going to win another championship if they stayed together. They had a number of guys who were free agents, and would demand max contracts. These guys were OK, but they weren't game changing superstars (the only one of those the Mavs had was Nowitzki). Cuban and his management team decided to let them walk and sign other players who would be cheaper and shorter terms so they could get in on future free agents. The NBA is sometimes a waiting game like this, there are a small number of game changing talents and you have to develop your cap space carefully so that when they become free agents you have even the possibility to sign them. Cuban missed on Lebron and Bosh and Wade, he wants to be ready if Durant decides to leave OKC.