Geoffrey Stone's enormous Perilous Times: Free Speech in Wartime from the Sedition Act of 1798 to the War on Terrorism describes at length how the law came into being. In 1917, President Woodrow Wilson knew he was going to have trouble assembling support for a faraway war in Europe against Germany, the country from which 25 percent of newer immigrants to the United States had migrated. He asked for draconian powers to suppress dissent, including press censorship, a ban on incitement to "disaffection" with recruitment and mobilization for the war, and a power for the postmaster general to refuse use of the mails to dissenters. Congress balked; defeated the press censorship section; and watered down the "disaffection" and "non mailability" provisions in what was called the Espionage Act.
Note that the legislation turned out be about not what we think of as spying (espionage), but about the crime of impeding the executive power in time of war.
And so the law was employed during the First World War. Government propagandists spread atrocity stories about German war crimes and enemies within. Several thousand people were jailed for such offenses as objecting that young working men were being sent to fight for U.S. capitalist fat cats. War fever was essential to national mobilization and people of German origin and socialist war opponents had to suffer if they wouldn't bend to the patriotic fever storm.
After the war, US judges and leaders recognized that the war time repression of speech had constituted a breach of national aspirations toward liberty. Most of the dissenters who had been given decades long jail terms were freed. A companion Sedition Act was repealed; but the Espionage Act stayed on the books.
Stone sums up his discussion of the era this way; it is interesting in light of current developments.
On the morning when we learn that the current Supreme Court has "disemboweled" (Lemieux again) the Voting Rights Act, it is easy to fear that our court may also prove "simply wretched" in relation to the government's current Espionage Act prosecutions.