EDIT: typo
The announcement of a merger has major market effects, which tend to improve the standings of one company and not the other; the announcement of that merge failing will have similarly large market effects. The breakup fee is simply a term in the negotiated contracts that specifies what happens if the merge fails to go through; the company whose market standing will fall as a result of the merge failure would negotiate to get paid a breakup fee.
There is no special law or anything covering it that I'm aware of, it's just that T-Mobile says "This will be a distraction, and there is serious risk that it won't happen. If you want us to take that risk, give us some insurance"
Apparently Sprint wants this deal badly enough to risk losing $1B if it doesn't go through, and T-Mobile was canny enough to realize this fact and negotiate for that clause in the contract.