(For a comprehensive look at how guns have been portrayed in the hands of Batman through the decades, check out this excellent article in the Silver Age Comics blog.)
I always admired Batman's self-imposed "no gun" rule, which came about later in the character's evolution. In the early days, when Bob Kane first invented the character, he occassionally pulled out a pistol against the bad guys.
It always seemed wrong to me, though. Bruce Wayne became emotionally and psychologically scarred when as a child he saw both his parents gunned down, shot to death in an alley before his eyes. That traumatic moment fueled his obsession to combat crime and strike fear in the hearts of evildoers. But with all his human skills, all the arsenal in his utility belt, and all the other cool gadgets at his disposal through his resources as a playboy millionaire, why would he resort to wielding a gun, the same weapon that killed his mother and father and drove him to roam the streets and skylines of Gotham City as a masked avenger? Carrying a gun, let alone using one, seemed to imply to me that Batman had become almost no different than the lawless men and women he tried to bring to justice.
The "Year Two" storyline examined Batman's temptation to use a gun to level the battlefield against villains who had no moral restrictions about the weapons at their disposal and no remorse about the innocent lives that perished in the crossfire in their pursuit of wealth, power, or anarchy. The story showed how Bruce Wayne came to the decision not to use guns in his war on crime.
Batman takes the law into his own hands in a city where the criminal element is beyond law enforcement's capacity to control. Since that itself raises the question of whether the character is really a hero or a crazy lawbreaker himself, Bruce Wayne has set self-imposed rules to govern his actions as he tries to justify his mission as a costumed crimefighter, and his ban on using guns is one of those moral guidelines.
When such rules are made, writers love to try to find ways to creatively break them. Superman's "code" not to kill, for example, has been pushed to the limit and broken, and likewise Batman's no-gun-policy has been ignored at times, usually unintentionally, but sometimes very intentionally, for shock value or to try to make a point.
The recent "Final Crisis" storyline had Batman use a gun with the intent to kill an archenemy, resulting in the apparent death of Bruce Wayne. In the subsequent storyline, other characters struggled to determine if someone else should carry on the Batman legacy, until finally Dick Grayson, the original Robin, donned the cowl as the new Batman. It was interesting to see that the choice to use a gun had such major repurcussions.
His origin is entwined with the tragedy that guns in the wrong hands can bring, so it is fitting that writers try to delve into more complex storylines that do not shy away from talking about guns in society instead of just using them as props for shock value and gratuitous excess. In a medium that regularly glorifies violence, and brandishes guns with often reckless abandon, it is good to see that the symbol of the gun when used with Batman still causes people to pause and think.