A contract is legally-binding; everyone knows that! So breaking a contract must be illegal, right?
The weird thing about contracts is that even though they are governed by laws (i.e. they can't include stuff that breaks city, state, or federal statutes) they aren't laws in and of themselves.
This makes sense when you think about it. For one thing, only the government can create laws, while anyone can create a contract because a contract is just a mutual agreement between people. And everyone is supposed to obey laws whether or not they agree with them, while the only people who are expected to adhere to contracts are the people who intentionally signed on to them.
When people sign contracts they take on additional, explicitly-stated obligations that go beyond what is provided for under the laws of the land. And these obligations are specifically related to exchange of value: money for labor, goods for services, etc.
So when someone breaks a contract—i.e. when one side fails to do what it said it would—what gets broken is the promise to engage in the mutually-agreed upon exchange outlined in the contract itself.
See you in court
Although contracts aren't laws and breaking them isn't strictly "illegal," signing on the dotted line does create legally-binding obligations.
What makes these obligations "legal" is the fact that the judicial system can be used to enforce contracts that get broken: the dreaded "see you in court" that accompanies a deal-gone-bad.
But since the scope of obligation covered by contracts is exchange of value—as opposed to the more general social obligation laws represent—enforcement of contracts is pretty much limited to financial compensation. You can't be thrown in jail for breaking a contract, but you can be sued for "breach of contract" by the other side if they lost money because of your actions (or inactions).
Referee, make the call
So if contracts aren't laws, why do courts get involved if they're broken?
The logic behind the court's role in contract enforcement is that the government wants people and businesses to do deals with each other, but people are reluctant to make deals with those they don't know if trust is all they have to go on.
Enter government. When someone breaks a contract, the other side of the deal can ask the government to step in on its behalf to enforce the contract. In this way, the government lends its authority to what would otherwise just be an agreement between individuals, with no built-in enforcement mechanism.
Honor amongst thieves, friends, et al.
If people couldn't rely on the government to enforce their contracts, private enterprise would be much riskier to conduct. This is the idea behind the phrase "honor amongst thieves": the courts obviously won't enforce agreements based on illegal activity, so thieves have to just cross their fingers and hope that "honor" will keep their business partners, well, honest.
Of course, it's not just "thieves" who operate this way: friends and family members do it all the time. We make countless informal deals with people we trust because we assume enforcement won't be necessary.
In fact, anyone can operate this way. People can choose to only do business with those they trust, and even when contracts do get broken people can decline to enforce them, choosing to take a loss rather than involve the courts.
This may seem like bad business sense, but it's precisely the difference between breaking the law and breaking a contract: with contracts, the decision to enforce is in your hands.