
Taylor was born to a prominent family of planters who migrated westward from Virginia to Kentucky in his youth. He was commissioned as an officer in the U.S. Army in 1808 and made a name for himself as a captain in the War of 1812. He climbed the ranks establishing military forts along the Mississippi River and by the time the Black Hawk War began, he was a colonel in 1832. His success in the Second Seminole War attracted national attention and earned him the nickname "Old Rough and Ready".
In 1845, as the annexation of Texas was underway, President James K. Polk dispatched Taylor to the Rio Grande area in anticipation of a potential battle with Mexico over the disputed Texas-Mexico border. The Republic of Texas had established independence from Mexico in 1836. In anticipation of the annexation of Texas to the United States, Taylor had been sent to Fort Jesup in Louisiana in April of 1844. His orders were to guard against any attempts by Mexico to reclaim the territory. He served there until July 1845, by which time annexation had been approved by Congress. President Polk directed him to deploy into disputed territory in Texas, "on or near the Rio Grande" near Mexico. Taylor chose a spot at Corpus Christi, and his Army of Occupation encamped there until the following spring in anticipation of a Mexican attack.
Taylor's men advanced to the Rio Grande in March 1846. Polk had attempted to negotiate with Mexico for the purchase of territory, but those attempts had failed, and war seemed a distinct possibility. Violence broke out several weeks later, when some of Captain Seth B. Thornton's men were attacked by Mexican forces near the river. Polk, learning of this, told Congress in May that US soldiers had been attacked on US soil. This wasn't exactly true, since the place where they were attacked was in disputed territory. Polk was able to obtain a declaration of war.
Taylor commanded American forces at the Battle of Palo Alto and the nearby Battle of Resaca de la Palma, defeating the Mexican forces, which greatly outnumbered his own. These victories made him a popular hero, and within weeks he received a brevet promotion to major general and a formal commendation from Congress. The national press compared him to George Washington and Andrew Jackson, two generals who had ascended to the presidency. At the time Taylor denied any interest in running for office. "Such an idea never entered my head," he remarked in a letter, "nor is it likely to enter the head of any sane person."
In September, Taylor inflicted heavy casualties upon the Mexican defenders at the Battle of Monterrey. The city of Monterrey had been considered unassailable, but Taylor's men captured it in three days, forcing Mexican forces to retreat. Taylor was criticized for signing a generous truce, rather than pressing for a large-scale surrender, but he did so for the sake of his men, who had fought a number of difficult battles. Polk was incensed. Half of Taylor's army was ordered to join General Winfield Scott's soldiers as they besieged Veracruz. This may have been a punishment for the liberal truce with the defeated Mexicans at Monterrey and to prevent Taylor from gaining any more glory (and political capital.)
Mexican General Antonio López de Santa Anna discovered, through an intercepted letter from Winfield Scott, that Taylor had sent the majority of his men to join Scott's army. His remaining force of 6000 included only a few hundred regular army soldiers, and Santa Anna intended to take advantage of the situation. Santa Anna attacked Taylor with 20,000 men at the Battle of Buena Vista in February 1847, leaving around 700 Americans dead or wounded at a cost of over 1,500 Mexican. Taylor took advantage of certain geographical locations to gain the upper hand. The Mexican forces retreated, resulting in what was considered to be a victory for the outnumbered Americans.

Taylor remained at Monterrey until late November 1847, when he set sail for home. While he would spend the following year in command of the Army's entire western division, his active military career was over. In December he received a hero's welcome in New Orleans and Baton Rouge, and his popular legacy set the stage for the 1848 presidential election. He coasted to win the nomination as candidate for the Whig Party and defeated Lewis Cass in the presidential election, 163 to 127 in the electoral college.