From the Wikipedia article:
A popular story for the origin of the seventh-inning stretch is that on April 14, 1910, on opening day, 6 ft 2 in (188 cm), 350-pound (160 kg), President William Howard Taft was sore from prolonged sitting at a game between the Washington Senators and the Philadelphia Athletics and stood up to stretch, causing the crowd to feel obligated to join their president in his gestures.[4] This story is set at a far later date than the others, however, so he may only have given the presidential seal of approval to a longstanding tradition; the story that his physical problems forced him to stand up contradict this, but he might have just been waiting for the proper accepted time to relieve his pain; either way, he gave national publicity to the practice.
As to the name, there appears to be no written record of the name "seventh-inning stretch" before 1920, which since at least the late 1870s was called the Lucky Seventh, indicating that the 7th inning was settled on for superstitious reasons.
and this:
Whether a stretch was observed nationwide is not known, but later in 1869 the Cincinnati Commercial reported on a game that was played on the West Coast between the Red Stockings and the Eagle Club of San Francisco: "One thing noticeable in this game was a ten minutes' intermission at the end of the sixth inning – a dodge to advertise and have the crowd patronize the bar."
However, a letter written in 1869 by Harry Wright (1835–1895), manager of the Cincinnati Red Stockings documented something very similar to a seventh-inning stretch, making the following observation about the Cincinnati fans' ballpark behavior: "The spectators all arise between halves of the seventh inning, extend their legs and arms and sometimes walk about. In so doing they enjoy the relief afforded by relaxation from a long posture upon hard benches." Another tale holds that the stretch was invented by a manager stalling for time to warm up a relief pitcher.[3]