Many people have the custom to sway their bodies either side-to-side or forwards and backwards while reciting the Amida and studying Torah. What is the origin of this practice, and it is a legitimate custom?
The Rama (Rabbi Moshe Isserles of Poland, 1525-1572), in the Orah Haim section (Siman 48), records this practice as the custom of the "Medakdekim" – those who are especially meticulous in their Halachic observance (listen to audio recording for precise citation). He explains that swaying during Torah study is intended as a reenactment of Matan Torah – the giving of the Torah at Sinai – when Beneh Yisrael shuddered in trepidation upon beholding the Revelation. The basis of swaying one’s body during prayer is a verse in Tehillim (35:10), "All my bones shall say: Who is like You, O Lord!" One sways his body during prayer in order that all his bones join in giving praise to God, as indicated by this verse.
Thus, according to the Rama, this is indeed a laudable practice which should be observed by those who wish to follow Halacha at the highest standard. This was also the view of Rabbi David Abudarham (Spain, 14th century; listen to audio recording for precise citation).
There is, however, a difference of opinion among the scholars of Kabbalah as to whether this is a proper practice according to Kabbalistic teaching. Rav Menahem Recanti (Italy, late 13th century) was of the opinion that swaying is indeed encouraged by Kabbalah. Others – including Rabbi Menahem Azarya de Fano (Italy, 1548-1620) and the Hid"a (Rav Haim Yosef David Azulai, 1724-1806) – claimed that Kabbalah opposes this practice, and this appears to have been the position of the Arizal (Rav Yishak Luria of Safed, 1534-1572).
As for the final Halacha, the Hid"a writes that although it is preferable not to sway, and rather to stand straight as one would before standing before a mortal king, those who do sway during prayer certainly have sources on which to rely. One’s primary concern, the Hid"a writes, is to pray with concentration. Similarly, Rav Yaakob Haim Sofer (Baghdad-Israel, 1870-1939) writes that one should certainly not criticize those who sway during prayer, as they have a basis for this custom. It should be noted that Rav Moshe Feinstein (Russia-New York, 1895-1986) reportedly prayed the Amida standing straight and upright like a soldier, rather than swaying. Many others, however, follow the practice of swaying, and thus although it is preferable to train oneself to pray without swaying, it is certainly an acceptable practice.
According to all opinions, however, one should not sway in an exaggerated fashion, as this would appear as a mockery of the prayer service. One must also refrain from swaying in a manner that calls attention to himself and makes a spectacle in the synagogue. Swaying is allowed only if it is done in a tasteful, respectable manner and for the sincere purpose of praying with greater intensity and concentration.
Summary: Different views exist as to whether it is proper to sway with one’s body during prayer and Torah study, or if this should be discouraged. While it is preferable to train oneself to stand straight without swaying, those who sway certainly have sources on which to rely, but must ensure to do so in a respectable manner.