Most state laws say something like this:
Whenever any roadway has been divided into two or more clearly marked lanes for traffic in one direction, the following rule applies: A vehicle shall be driven as nearly as practicable entirely within a single lane and shall not be moved from the lane until such movement can be made with reasonable safety.
You can be cited for an unsafe lane change under the rule that prohibits unsafe turns, or under this more specific violation, which has the following elements:
Police will readily and properly pounce on a driver who weaves in and out of traffic without signaling, especially if that driver cuts off another car, forcing it to abruptly brake. Often the officer will watch for brake lights flashing on cars in your vicinity to determine if you are endangering others with your frequent lane changes. Your defense to this one should often be based on the fact that there can be many reasons for a driver to touch the brakes, and that your lane change may have had nothing to do with it. For example, a car could have been traveling much faster than the speed limit before you pulled into its lane, causing it to brake sharply. Or a car in front of you may have braked sharply, forcing you to make a quick lane change. Because all these circumstances are difficult to see from a distance or a bad angle, you'll have a good chance to sell the judge on why your lane change was safe under the circumstances.
Establish where the officer was when you changed lanes. Most drivers are on their best behavior when a police cruiser is close. So officers end up issuing many violations that they view from a distance, meaning their ability to judge the nuances of whether particular conduct was safe or not is poor. If this was true in your case, use your chance to cross-examine the officer to ask questions like these:
"How far behind the other vehicle were you when you observed it brake?"
"After I changed lanes, you were behind the other car, weren't you?" [If "yes," follow up with] "At that moment, you couldn't see the distance between my car and the other car, could you?"
"I assume you weren't pacing the vehicle you claim I turned in front of?" [If "no," follow up with] "Then you don't know how fast it was going, do you?"
Then work the officer's admissions into your testimony that your conduct was safe and that the officer's conclusion that it wasn't isn't reliable.
EXAMPLE: You're driving 65 mph on the freeway in the right-hand lane. To avoid cars entering from an upcoming on-ramp, you decide to move into the center lane. Preparing to change lanes, you flick on your left turn signal and look in your rear- and side-view mirrors. You see another car in the middle lane, but it's about eight car lengths back, so you make the lane change. As you do, the apparently inattentive driver of the other car speeds up, with the result that your vehicles are very close together. You are ticketed for an illegal lane change.
In court, explain that you were going at or near the speed limit and were changing lanes to avoid a dangerous merge situation. Also, testify—if true—that just after you signaled your lane change, the other driver sped up. (Since the other driver won't be in court, only the police officer will be there to contradict you.) In short, any unsafe conditions were brought about by the other driver's unsafe conduct, not your lane change.