No one wants to find himself on the wrong side of the law, but if you get arrested, you may be able to beat the case or at least have the charges downgraded. Following an arrest, the district attorney will, if he decides to pursue the matter, try to build a case against you. Sometimes, you may have a misdemeanor charge reduced to an infraction, which is a non-criminal violation that won't result in a criminal record. However, the decision is under the discretion of the prosecutor. The key lies in convincing him to charge you for the lesser offense.
Hire an attorney. Not everyone can pay for one, but if you can, hiring a lawyer is the best defense and represents your greatest chance of having the charges downgraded to an infraction. The legal system also provides court-appointed counsel for indigent (poor) defendants who can't pay for their own legal services.
Schedule a meeting with your lawyer and the district attorney trying your case. Prosecutors routinely meet with defense attorneys and defendants prior to a case.
Try to convince the prosecutor that his case against you is weak. This involves myriad points of fact and law that depend on the particular circumstances of your case and the jurisdiction in which it is tried, but if you can demonstrate that the evidence against you is unconvincing, you may be able to persuade the prosecutor to downgrade the charges.
Make an offer for a plea bargain, in which you will face less serious charges (i.e. an infraction rather than a misdemeanor) in exchange for pleading guilty. Such arrangements are advantageous to prosecutors as well because it saves them the expense and hassle of preparing for a trial and also spares them the risk of a case ending in acquittal.