What prompted the US Congress to write such a law? What was it aiming to accomplish?
First, for the benefit of other readers, let's cover what the National Firearms Act of 1934 (or NFA) is and what it does. The NFA was the first substantial US federal gun law. Prior to this, gun laws were handled almost entirely by the states, and while some states did flex those muscles (New York passed its strict Sullivan Act in 1911, for example), a person in a state with very liberal gun laws would face no legal restrictions to speak of on obtaining any firearms at all.
The NFA of 1934 was a restriction not on the transfer and possession of firearms in general, but in particular on weapons which were believed by its proponents to be especially dangerous. It required them to be registered with the federal government, and assessed a tax on their transfer that was intended to be burdensome to prohibitive. The exact scope of these restrictions can be very complicated (there's a category called "any other weapon" that causes confusion even today even among many gun enthusiasts), but for the sake of this question we can say that for practical purposes the NFA restricts mostly silencers, machineguns, and rifles and shotguns under a specified length. (Today "NFA items" also includes things like very large-bore firearms and weaponized explosives, but these were added when the NFA was amended by the Gun Control Act of 1968, so they're not relevant to your question.) There are some exceptions and complicating factors, but for most purposes these items must be registered, a $200 tax must be paid on each act of manufacturing or transfer (a very large sum in 1934), and the owner must get approval before moving the items across state lines.
So what prompted this law in the first place? In the 1930s, Congress was looking for a way to address the common perception that existing law enforcement practices were unable to deal with "motor bandits," the popular image of which was a group of robbers armed with powerful weapons and transported by automobile. With the relatively new availability of automatic firearms and motor vehicles, there was a perception that existing local and state police who had traditionally dealt with violent crime were unable to respond quickly enough and with sufficient force to stop these especially dangerous robbers, and a federal solution was sought.
In his 1986 Cumberland Law Review article on the legal-historical background of the Firearms Owners Protection Act, attorney David Hardy sums up the context of the NFA thus (citations omitted in my excerpt):
The late 1920s and early 1930s brought, however, a growing perception of crime both as a major problem and as a national one. Public officials did much to support the perception; Attorney General Homer Cummings, for instance, publicly estimated that America was being terrorized by half a million armed thugs, a force larger than the contemporary United States Army. The mobility of the automobile enabled criminals, in those pre-police radio days, to move between jurisdictions before police units could generally be alerted; such criminal gangs found the submachinegun (a fully automatic, shoulder-fired weapon utilizing automatic pistol cartridges) and sawed-off shotgun deadly for close-range fighting. The resulting quest for law enforcement solutions approached the incredible. At one 1933 hearing, for instance, a Senate subcommittee heard, with no recorded skepticism, calls for a ban on felons riding in automobiles, universal fingerprinting of all citizens, mandatory "papers" for interstate travel, and enactment of national vagrancy laws authorizing warrantless search and arrest of anyone "reputed" to "habitually violate" the laws (with law enforcement officials to testify as to the arrestee's reputation). On a more practical plane, the Department of Justice proposed what became the National Firearms Act of 1934.
The original draft of the NFA was substantially stricter than what ended up being passed. The "short barreled" rifle and shotgun restrictions were orginally part of a larger restriction of all concealable firearms (which consisted of registration and a five-dollar tax on handgun transfers, with the short rifle and shotgun restrictions intended to cover handgun alternatives as well), and a definition of "machinegun" that included "any weapon capable of firing twelve or more shots without manual reloading" (a definition which would today include the vast majority of semiautomatic firearms).
Testimony and advocacy by General Milton Reckord (then executive VP of the National Rifle Association and acting on its behalf) resulted in the removal of handguns from the Act, and in a machinegun definition closer to the common meaning of that term ("any weapon which shoots[...]automatically more than one shot, without manual reloading, by a single function of the trigger").
The quid-pro-quo of the debates refined the NFA toward this specific aim of disarming gangsters and bank robbers, as opposed to restricting the general civilian use of guns for sporting purposes and self defense. Throughout, Congressional advocates for the NFA consistently frame their concern in this way. The preparatory meeting of the House Ways and Means Committee on May 15 1934, for example, quotes the Attorney General as saying:
The development in late years of the predatory criminal who passes rapidly from State to State has created a situation which is giving concern to all who are interested in law and order. ... There lies the heart of our problem. The roaming groups of predatory criminals who know ... that they are safer if they pass quickly across the State line, leaving the scene of the crime in a high-powered car or by other means of quick transportation.
Similarly, the Senate Commerce Committee hearing of May 28 1934 contains many references to brazen motor bandits in general and prominent criminal personalities in particular, such as in this exchange between an NFA advocate and the NRA representative, discussing a potential restriction on ammunition sales:
Senator Copeland: All right. I Approve that.
Now, what about the ammunition?
General Reckord: Well, we don't think the ammunition, as such, is dangerous.
Senator Copeland: Do you think it is proper for one Dillinger to be able to go in any ammunition shop in the United States and buy all the cartridges that he wishes?
There were of course also contemporary desires for broader federal gun control legislation that went beyond the particular specter of John Dillinger and his peers; the Federal Firearms Act of 1938 licensed and regulated all firearms dealers, and the debate would continue throughout the period discussed here on r/askhistorians (most significantly with the current licensing system instituted by the Gun Control Act of 1968 and refined by the Firearms Owners Protection Act of 1986). But when discussing the National Firearms Act of 1934 in particular, the stated motivation was explicitly the belief that high-profile violent criminals were using machineguns, sawed-off-shotguns, and motor vehicles to overwhelm the law enforcement of the time.