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. (It doesn't help their reputation that Star Wars blaster shots fill the screen with lines of light, so we're much more aware of the number of shots fired than we would be with muzzle flashes and occasional bullet thunks into scenery.) However, that still does leave some unexplained scenes where the Stormtroopers were far worse shots than the protagonists.
Besides the impressive amounts of Plot Armor covering pretty much every inch of a named, major character, there are a few In-Universe facts one can use as a Handwave. According to some Star Wars Legends 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. (Which doesn't explain why Han, Luke, and Leia were able to shoot quite accurately with Stormtrooper rifles in the Death Star and Cloud City...) 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. Another explanation is that by the end of the Clone War, the casualty counts for the Clone Troopers was getting out of hand so Palpatine asked Spaarti-cloners to make troopers as well; Spaarti clones take a year to grow (a LOT less time than the 10-year Kamino clones) and are taught through something called "flash-training" in combat; and only the basic stuff (how to fire a rifle, etc), meaning they never received formal training in ANY way. And as time went on, the Spaarti clones began replacing the Kaminoan Clones. The video game Star Wars: Battlefront 2 gave the reasoning as that most of the clones were actually stationed on the Death Star (or at the very least, a good bit of Vader's 501st) and when it went up, so did the clones.
On the other hand, Cracked 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 civilians to death.)
As for the Clone Troopers' superiority over the Stormtroopers, the Clones almost certainly didn't have to suffer from hardware as shoddy as their successors did, presumably due to better funding for the military during full-on-war than during a police action against a comparatively small insurgency. (Recall that Clone Troopers in the swamps of Kashyyyk got to wear camouflage armor, then 20 years later Stormtroopers in the forests of Endor had to wear the standard, blindingly white armor.) It also helped that the Clone Troopers were cloned from known Bounty Hunter Jango Fett, while the Stormtrooper ranks were diluted with soldiers from far-less-badass sources, like recruits, conscripts and non-Jango clones.
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, Super Battle Droids and Commando Droids, do pose a threat in later episodes, but even so, they tended to only succeed against Jedi in situations where the Conservation of Ninjutsu 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. It is at this point that Watsonian analysis must either invoke the Force, or wash its hands of the matter and walk away. Though there is the fact that most people without lightsabers are shooting back, which makes than an immediate threat and harder to aim at calmly.