Also, watching footage of depth charging ops, the charges seem to be aimed very close to the ship. Were subs really that close when attacking shipping?
The effectiveness of depth charges depended heavily on three factors: the sensors used to find the submarine, the size and composition of the explosive charge, and the time the charge took to sink. The first limited charges in three main ways. Firstly, submarines could only be attacked if they could be found - difficult to do without sonar/Asdic, as the RN and USN found in WWI. Secondly, localising the submarine well enough to attack could be difficult with a poor sonar, or with a ship that was moving fast enough to degrade sonar performance. Finally, the shape of the sound beam produced by the Asdic sets used meant that as an escort closed on a submarine, the contact was lost. This allowed the submarine to manoeuvre to avoid the depth-charge pattern. This was exacerbated by the last factor, as a depth charge that sank slowly gave the target submarine more time to evade. Finally, the size and composition of the charge determined how much damage it would do to a submarine, and at what distance.
The RN used, over the course of the war, three main depth charges. These were the Mark VII, the Mark VII 'heavy' and the Mark X, or 'One Ton' charge. The Mark VII, introduced in 1939, had a charge of 290 lbs of Amatol explosive. It was expected to have a lethal radius of ~30 feet, but in service, this turned out to be more like 20 ft. It sank at a speed of 7-10 ft/sec, making it a rather ineffective weapon in conjunction with the poor early Asdic sets. The Mark VII 'heavy' was an upgraded version introduced in late 1940. It had an additional cast-iron weight to increase the sink rate and, from 1942, a charge made from Minol. The cast-iron weight increased sink rate to 16 ft/sec, while the Minol charge was lethal out to 26 ft. The Mark X could only be launched from a 21in torpedo tube, limiting the deployment to just destroyers, but it was far more effective. It had a sink rate of up to 21 ft/sec, depending on precise model, and a 2000 lb charge, from which it took its name. This charge was, apparently, as lethal as a pattern of 10 Mark VIIs. These were paired with improvements in Asdic design. One of the most significant was the 1943 Q attachment, which allowed older Asdics to hold contact to very close ranges. The other significant development was the Type 144 Asdic, which also entered service in 1943. This was a highly capable set, much more sensitive than previous designs, and with better target classification properties. While I have considered solely lethal radii here, it's worth noting that depth charges were still effective outside these ranges. Comparatively distant explosions could still damage sensitive equipment like periscopes, while closer but non-lethal ones could do serious damage to a sub. Constant attacks, even if they didn't sink a sub, were extremely trying to crew morale, and typically led to the captain choosing to break off his attack.
However, depth charges were not the only weapon the RN had against submarines. Due to the limitations of the depth charge-Asdic system, alternatives were sought. The solution to the 'dead zone' problem was to introduce a weapon that threw charges ahead of the ship, while the target sub was still illuminated by Asdic. Some early trials had been carried out in the early 1930s aboard HMS Torrid, but this was a failure, and the weapon was removed by 1934. In WW2, the first ahead-throwing weapon was codenamed Hedgehog, introduced in late 1942. This was a spigot mortar that fired a circular spray of bombs over the front of the ship. The typical Hedgehog installation fired 24 bombs, each with a weight of 65lbs in a forty yard circular pattern, with the centre 200 yards ahead of the ship. Hedgehog was effective, but limited by the fact that it used contact-fused bombs, so it could only destroy a submarine it impacted directly. From 1943, it was succeeded by Squid. This was a three-barrelled mortar, firing 200lb depth charges up to a range of 275 yards. Squid was combined with the Type 147 Asdic, which could automatically set the depth setting on the depth charges. Squid was deployed in either single, or double installations.
Comparing the effectiveness of these systems is easily done. The RN kept thorough records of the number of attacks with each weapon, and the number of sinkings attributed to each. In the early part of the war, depth charges were relatively ineffective, especially due to the poor quality of the available Asdics. Over the first six months of the war, 4000 attacks were made with depth charges. These resulted in only 33 sinkings. However, by 1943, depth charges had about a 5% success rate. Between January and June 1943, 554 depth charge attacks were made, causing 27.5 sinkings (including shared kills). Over the last two years of the war, depth charges made 5,174 attacks, causing 85.5 sinkings. Hedgehog and Squid were much more effective, but less widely deployed. Over the same period, 268 Hedgehog attacks were made, for 47 kills. Double Squid was used in just 27 attacks, but sank 11 submarines. Single Squid was rarely deployed, but from RN testing, was expected to have a 25% sink rate.
The reason you're seeing depth charges aimed close to a ship is that depth charges were a short-range system in any case. During their first deployments in WWI, depth charges were simply just rolled over the back of the attacking ship. Throwers that extended their range were soon developed. However, even in WWII, charges were still rolled over the stern from racks. The depth charge throwers, either the RN's Mark IV and V, or the USN's K gun, were still close range weapons. The Mark IV could throw the Mark VII charge out to 67 yards, or the 'heavy' version to 51 yards. The K gun had a range of 68 yards with the Mark VII, while the Mark V had a range of 78 yards with the same weapon. Submarines could attack from much longer ranges - the typical German G7a torpedo had a range that varied from 6,500 yards to 15,300 yards depending on setting, although, as /u/kieslowskifan points out, attacks were typically made at ranges of ~1000 yards or below. However, it must be remembered that their targets were typically not the escorts with the depth charges. The merchants or capital ships were at the centre of a large screen of escorting ships, putting the escorts closer to their targets than the submarines were to their prey.
Edit - Sources:
Atlantic Escorts: Ships, Weapons & Tactics in World War II, D. K. Brown, Seaforth Publishing, 2007
Nelson to Vanguard: Warship Design and Development 1923-1945, D. K. Brown, Seaforth, 2012
Depth Charge: Royal Naval Mines, Depth Charges & Underwater Weapons 1914 - 1945, Chris Henry, Leo Cooper Ltd, 2005
River-class Frigates and the Battle of the Atlantic: A Technical and Social History, Brian Lavery, National Maritime Museum, 2006
While submarines did get close to their targets,the footage of depth charge attacks gives a false impression of very close proximity fighting. The German U-boat torpedo attack envelope was between 300 and 1000 meters, with operational manuals stressing that no torpedo attack should take place below 300 meters. The closest recorded U-boat attack was U-435, whose war diary indicated she fired a torpedo at 225 meters on 30 December 1942. The footage of depth charge attacks is not the same as a submarine attack. The common ASW tactic for depth charges in this period was for the escorts to race to the reported position of the submarine and saturate the area with depth charges. Allied ASW became more sophisticated over the course of the war, but the depth charge itself is a crude weapon and some of the tactics used for depth bombing were quite rudimentary, especially in the early part of the war. ASDIC (aka sonar) might give an escort a reading after receiving either a visual or radar signal of a submarine on the surface or submerging. The escort would then steam to the area and attack. Fixing the submarine's position was largely guesswork in the early stages of the war, and by the later part of the war had become more educated guesswork. Even as the Allied ASW deployed more sophisticated forward-throwing weapons like Hedgehog and Squid and had better sensor and wartime experience, these ASW weapons still relied upon mass quantities to destroy a submarine.
The effectiveness of depth charge was quite low given the number used. David Brown estimated that in the first six months of the war, the effectiveness of depth charges was around 1% in the Atlantic theater, while by 1943-44 the chances of a successful depth charge attack was around 5%. Fraser M. McKee likewise came up with a similar rate of return. The Navweapons website maintains that the effectiveness of was slightly higher. It is difficult to parse out the exact depth charge/sinking ratio because not only were they a saturation weapon, but often multiple escorts would engage in attacks and other weapons would be used like Hedgehog as the ASW methods advanced. Additionally, submarine sinkings are difficult to confirm even with the benefit of Axis shore logs of positional reports and losses and cross-referencing those with Allied claims.
The reason for poor showing of depth charges were manifold. For one thing, the escorts' sprinting to the reported submarine denuded the ASDIC picture and often the escort captain was going into the attack blind. Depth charges also lost their effectiveness at greater depths, and submarine commanders would often take their boats deeper for safety. More advanced sensors, teamwork, and greater explosives like Torpex made depth charges more effective, but they still had to get within a very close radius to do serious damage to a submerged submarine. Finally, and perhaps most importantly, the time it took between the initial escort attack and for the depth charge to drift down to the estimated position of the target provided a submarine an important window to escape detection. This "dead time" coincided with some of the sensor blindness and a veteran submarine captain could maneuver his boat out of harm's way.
Yet the effectiveness of depth charging was not just measured in the number of submarines sunk. Sustained depth charging could loosen submarine fitting and damage equipment. Additionally, explosions limited hydrophone effectiveness, so the submarine lost its picture of the surface. By forcing the submarine to stay submerged and maneuver, the escorts forced commanders to run down batteries and the slower submerged speeds meant they would be out of position to renew their attacks on a convoy the longer they stayed submerged. Sustained attacks were also detrimental to morale and psychological performance of the submarine crew.
Finally, the most attractive characteristic of depth charges was their cheapness. Depth charges did not necessitate highly specialized equipment and a variety of hulls could accommodate their rails and throwers. Even ineffective depth charging could be a larger victory as they denied the submarine a chance to renew its attacks. As the wartime navies struggled to maximize their escort fleets, even a relatively ineffective weapon had its merits.
Sources
Brown, D. K. Atlantic Escorts: Ships, Weapons & Tactics in World War II. Barnsley: Seaforth Publishing, 2007.
McKee, Fraser M. "An Explosive Story: The Rise and Fall of the Common Depth Charge." The Northern Mariner 3: 45-58.
Morgan, Daniel, and Bruce Taylor. U-Boat Attack Logs: A Complete Record of Warship Sinkings from Original Sources 1939-1945. Barnsley: Seaforth Publishing, 2011.