Yes, it’s legal—you don’t actually give up the right (as DrBill notes) but you can agree to certain terms on your use of it. For instance, I might agree to pay you $100 a month if you don’t advocate for Ron Paul’s brand of libertarianism. You still have the right to free speech, but if you do, you’re breaching the contract, and you give up the right to sue me for the money I agreed to give you. Alternately, if I find out that you were speaking out in favor of libertarianism in a month in which I paid you, I can sue you to recover that $100 on the grounds that you were in breach of your contract.
Adding collective bargaining to the mix doesn’t change that. All you’re doing is empowering someone else to negotiate on your behalf; once the negotiation is done, you get to decide whether to sign the contract or not. If you sign it, you’re bound by what the negotiator worked out; if you don’t sign it, you’re out of a job. If the negotiator doesn’t have the same priorities as you in negotiation the contract, that’s an unfortunate circumstance, but in the end you get to decide if you want to abide by the contract.