This is the reason for Attorney Client Privilege. One of the first things any decent lawyer will tell a new client is to tell them their side of the story and do not lie. Defense Attorneys are legally bound not to release any information related to the client's personal buisness or activites. You can get disbarred for violating this. This holds for prospective clients, current clients, and former clients/prospective clients.
There are exceptions, namely if the client is seeking legal advice related to a planned but not executed crime.
From here, the defense lawyer will advise his client on how to proceed with the case. If the client wants to plea not guilty despite 10 eye witnesses, a security camera, a news media crew reporting on a separate incident, and the cops finding him with the still smoking gun and blood of the victim all over the hands, the Lawyer needs to discredit why the client "didn't do it".
A lot of this is based on the impeach-ability of the evidence. The more you can discredit the more you can get away with. This is actually difficult, but not impossible. Consider the Kasey Anthony (Casey) where the prosecution was trying to prove a mother had murdered her daughter and dumped the body. Here the fact that the girl was dead and Anthony's initial statements were bold face lies were suspect from the get go... but the stories from testimony and evidence and media were so bizare and out there, that it was hard to tell the exact timeline of events at all. The mother got off despite much of the nation pretty certain she did it because, no one could figure out what really happened.
The "Proof beyond reasonable doubt" standard for conviction means that all plausible explanations counter to the prosecution's narrative can be discounted because the evidence does not support the alternative happening. However, if you cannot believe one side over the other, then the jury is instructed to plead innocent, even if there is certainty in the fact that they did it in one of two ways.
Fun Fact: Scottland gets around this by offering a third finding "Not Proven" which is why their cases can get bizarre. This means that the accused is released not because he is clearly innocent but because the prosecution sucks at their job. As one Scottish observer noted, it's the jury basically saying "You did not do it, and you better not do it again."
However, courts rarely reach this in the United States, because the US is one of the few countries that tolerates plea bargaining and these deals live as a gamble that the prosecution can get the guilty to admit to lesser crimes in exchange for not taking the prosecutor through the trial phase. 95% of US criminal cases are decided before the trial even starts and this is largely based on what evidence gets allowed to the trial. A drug dealer's case is far more likely to be decided based on a pre-trial motion to dismiss the seized drugs as evidence (you know, the basis of the charge) than it is by a trial. If the evidence is dismissed, the prosecutor could decide not to press the matter cause they have to prove that the accused was in possession of evidence that he cannot show the jury. Conversely, a defense lawyer might strongly recomend that his client take a deal in order to minimize jail time. And this is why the Defense Attorney still needs to help his client.
The defense attorney isn't supposed to give two damns about about anyone accept his client. If the client does not have the tools to be honest with him, then he cannot do that. In the drug scenario, if your client is the small fish who sells for the big city kingpin drug dealer, it's in your clients interest to flip on his boss in order to escape the charges against him (or get a lighter sentence or lesser charges).
Again, with attorney client privilege, the defense also is under no obligation to release any evidence of guilt to the prosecution. The reverse is not true. The prosecutor must release everything they will use against the accursed and even stuff they won't. The defense is entitled to know if the prosecution has evidence that works in favor of the defense and can get into big trouble if they do not (see the Prosecutor in the Duke Lacrosse scandal for a great example of a guy who did not do it).