Imperial Stormtrooper Marksmanship Academy/Analysis
So why were the Stormtroopers in Star Wars such bad shots?
First of all, the Stormtroopers' reputation for being bad shots is a bit exaggerated. They do quite well against the Rebels at the beginning of A New Hope and in the Hoth battle in The Empire Strikes Back. For example, they can hit rebel soldiers from at least ten meters away simply by hip-firing. It's mainly in the presence of Plot Armor-wearing main characters that their aim starts to degrade. And in A New Hope, the Stormtroopers were under orders from Tarkin to let Luke and his pals escape so that they could be tracked to the Rebel base—so the troopers (or at least the most fanatical ones) were trying to miss. And in the one scene where the troopers were trying to take prisoners, they were able to hit Princess Leia with a stun gun on their very first shot. However, that still does leave some unexplained scenes where the Stormtroopers were far worse shots than the protagonists.
According to some Expanded Universe sources, the standard-issue Imperial rifles were defective and all-but-impossible to aim with, and the Imperial administration was too cheap and lazy to fix this known defect. While Han, Luke, and Leia have all been shown as being quite accurate with stormtrooper rifles, its important to note that the first is one of the galaxy's greatest gunslingers and a man whose aim is apparently so instinctive he doesn't even bother to use the sights, and the latter two are Force-sensitive. Fanon also speculates that Stormtroopers' helmets restrict their vision (recall Luke's "I can't see a thing in this helmet!") or have shoddy targeting.
On the other hand, Cracked.com offers an interesting psychological explanation, pointing out that the Stormtroopers' on-screen accuracy is consistent with Real Life studies demonstrating that people (even trained soldiers) have inhibitions against firing at individuals with visible faces (like Luke and his pals), but fewer inhibitions against firing at others who can be dehumanized (like faceless Jawas or uniformed Rebel soldiers). The Stormtroopers' own face-concealing armor didn't do them any favors in this equation.(Then again they didn't have a problem burning two unarmed civilains to death)
Another explanation is that Stormtroopers mostly were armed with carbines, not proper rifles, and this means for reasonably trained normal people effective range is going to be fairly short no matter what; for those Force sensitive and/or talented gunslingers it's perhaps no cakewalk either, but they have an advantage, and Chewbacca's "crossbow" thing falls under long arms.
As for the Clone Troopers' superiority over the Stormtroopers, the Clones were not only superior genetic stock (all being clones of an elite Mandalorian trooper, Jango Fett) but didn't have to suffer from hardware as shoddy as their successors did. We know this from testimony by clonetrooper Rex CT-7567 in the Star Wars "Rebels" animated cartoon series, during a sequence where he is required to temporarily pose as a stormtrooper. As a retired officer of the original 501st Legion from the Clone Wars, he is intimately familiar with Old Republic clonetrooper gear and how it compares to the Imperial gear he's currently using. And as soon as he puts on the stormtrooper armor Rex goes from his normally excellent marksmanship to, well, shooting like a stormtrooper, and blames it on not being able to see a damned thing while wearing the shoddy helmet. As soon as he ditches the helmet, he gets back to his normal proficiency level.
And the battle droids? The canon explanation for their shoddy aim is that they really are that bad. The Trade Federation's battle strategy emphasized quantity over quality, seeking to overwhelm the opponent with huge numbers of disposable troops. The standard B1 Battle Droids were so disposable that, after Episode I, they were relegated to comic relief, rather than presented as a threat. Upgraded versions, like the Droidekas and Super Battle Droids, do pose a threat in later episodes, but even so, they tended to only succeed against Jedi in situations where the Inverse Ninja Law was working against the Jedi.
Now, for the really baffling part: When Stormtroopers or battle droids fire at a main character with a blaster, they tend to miss entirely. When Stormtroopers or battledroids fire at a main character who is a lightsaber-wielding Jedi, their shots are on-target or darn close—enabling the Jedi to deflect the shots (usually right back at their attacker) with said lightsaber. A possible explanation is that since the Jedi wants to deflect the blaster bolt, he is deliberately allowing it to come close enough that he can reach it with his lightsaber instead of dodging it entirely. Supporting evidence for this lies in the performance of Kanan Jarrus during the pilot episode of "Rebels" -- despite being only a Jedi padawan, who prior to this scene had not trained in the Force for years, he still proves capable of easily dodging blaster fire at close range without even drawing his lightsaber... and then, after he draws his lightsaber, deliberately stops dodging so that he can instead start parrying (and reflecting the blaster bolts back into the stormtroopers).