The Pledge of Allegiance, thought to have been written in 1892 by Francis Bellamy, was officially recognized by Congress only in 1945. “The Pledge ” was published anonymously by a magazine for young people, The Youth’s Companion on September 8, 1892, and was written in celebration of the 400th anniversary of the discovery of America. The published Pledge reads
“I Pledge allegiance to my Flag and the Republic for which it stands; one Nation indivisible, with Liberty and Justice for All.”
The Pledge was accompanied by instructions for a salute to be performed as part of the Columbus Day celebrations: “At the words, ‘To the Flag,’ the right hand is extended gracefully, palm upward, toward the Flag, and remains in this gesture till the end of the affirmation; whereupon all hands immediately drop to the side.”
The first flag salute statute [requiring children in public schools to recite the Pledge of Allegiance] was passed in New York in 1898, the day after the United States declared war on Spain. New York’s state superintendent, in his Manual of Patriotism, included five possible ‘patriotic pledges’ that teachers might use in their classes. One of these was Bellamy’s, but it was placed fifth.
In 1940, the US Supreme Court ruled in Minersville School District v. Gobitis that a local school board could expel students who refuse to recite the Pledge. Justice Felix Frankfurter wrote:
“So far as the Federal Constitution is concerned, it is within the province of the legislatures and school authorities of the several States to adopt appropriate means to evoke and foster a sentiment of national unity among the children in the public schools.”
In 1942, legislation was adopted by Congress “to codify and emphasize existing customs pertaining to the display and use of the flag of the United States of America.” The text of the pledge, as originally written and modified a bit by the National Flag Conference in 1923 and 1924, was inserted into this legislation (Public Law 829, Chapter 806, 77th Congress, 2nd session), but without designating it as the official pledge. (The small changes made to the text resulted in this version: “I pledge allegiance to the Flag of the United States of America and to the Republic for which it stands, one nation, indivisible, with liberty and justice for all.”) Congress also amended the Flag Code this year, substituting the original straight arm salute, associated with Nazi Germany, with the current salute of the right hand over the heart.
In 1943 the Supreme Court overturned the Gobitis decision in the case of West Virginia State Board of Education v. Barnette. Justice Robert Jackson wrote that the compulsory state action violated the First and Fourteenth Amendments, and that “Under the Federal Constitution, compulsion as here employed is not a permissible means of achieving ‘national unity.'” He famously added:
“If there is any fixed star in our constitutional constellation, it is that no official, high or petty, can prescribe what shall be orthodox in politics, nationalism, religion or other matters of opinion or force citizens to confess by word or act their faith therein.”
Then in 1945, additional legislation was introduced into Congress by Representative Herman P. Eberharter of Pennsylvania, which amended the 1942 act to give official congressional sanction to the pledge.
The words “under God” were added by Congress on June 14, 1954, in response to the anti-Communist (and thus anti-atheist) opinion sweeping the country during the Cold War. This addition to the law, sanctioned by President Eisenhower, is still controversial. (President Eisenhower said in signing the law, from “this day forward, the millions of our schoolchildren will daily proclaim, in every city and town, every village and rural schoolhouse, the dedication of our nation and our people to the Almighty.”)
The legislation for The Pledge is found in Title 4, Chapter 1, Section 4 of the U.S. Code.
The Federal legislation does not refer to schools; it is state and local law that mandates recitation of The Pledge in schoolrooms. Students may decline to participate, although as even the Supreme Court has recognized, the consequences could be deleterious. Schoolchildren of minority faiths, by so declining, would isolate themselves from classmates and open themselves up to ridicule and rejection.
The use of the phrase “under God” is still being contested and litigated. You can read more about it in this Smithsonian article.