The answer to that question is complex, involving issues of possible innocence, inaccurate evidence, overcharging by the prosecutor, guarding constitutional rights, false or untrustworthy testimony, ensuring a fair trial, protection from unfair laws and harsh/illegal punishment — and just keeping the government honest.
One of the better answers was provided some years ago by United States Supreme Court Justice Byron White in the landmark case of United States vs. Wade, 388 U.S. 218 (1967):
Law enforcement officers have the obligation to convict the guilty and to make sure they do not convict the innocent. They must be dedicated to making the criminal trial a procedure for the ascertainment of the true facts surrounding the commission of the crime. To this extent, our so-called adversary system is not adversary at all; nor should it be. But defense counsel has no comparable obligation to ascertain or present the truth. Our system assigns him a different mission. He must be and is interested in preventing the conviction of the innocent, but, absent a voluntary plea of guilty, we also insist that he defend his client whether he is innocent or guilty. The State has the obligation to present the evidence. Defense counsel need present nothing, even if he knows what the truth is. He need not furnish any witnesses to the police, or reveal any confidences of his client, or furnish any other information to help the prosecution’s case. If he can confuse a witness, even a truthful one, or make him appear at a disadvantage, unsure or indecisive, that will be his normal course.
Our interest in not convicting the innocent permits counsel to put the State to its proof, to put the State’s case in the worst possible light, regardless of what he thinks or knows to be the truth. Undoubtedly there are some limits which defense counsel must observe but more often than not, defense counsel will cross-examine a prosecution witness, and impeach him if he can, even if he thinks the witness is telling the truth, just as he will attempt to destroy a witness who he thinks is lying. In this respect, as part of our modified adversary system and as part of the duty imposed on the most honorable defense counsel, we countenance or require conduct which in many instances has little, if any, relation to the search for truth.
Some fine day, you or someone close to you will be arrested and charged with a criminal offense. That person may or may not be innocent, but you will pray that he or she is defended against the overwhelming forces of the government by a competent attorney.