The short answer
The first Mughal emperor was Babur, who founded the dynasty in 1526. The last was Bahadur Shah II, also known as Bahadur Shah Zafar, who was removed from the throne by the British in 1857.
Babur, the founder
Babur (1483–1530) was a prince from Central Asia, descended from Timur on his father’s side and, through his mother, from Genghis Khan. After struggling to hold territory in Central Asia, he turned toward northern India. In 1526 he defeated Ibrahim Lodi, the Sultan of Delhi, at the First Battle of Panipat. He took Delhi and Agra and ruled until his death in 1530. He also wrote a memoir, the Baburnama, which is a valued source on his life. His son Humayun succeeded him.
Bahadur Shah II, the last emperor
Bahadur Shah II (1775–1862) came to the throne in 1837. By then the Mughal emperor had little real power, and the East India Company controlled most of the region. During the 1857 uprising, rebel soldiers in Delhi looked to him as a figurehead. When the British recaptured the city, he was captured, tried, and exiled to Rangoon in Burma, where he died in 1862. He was also known as a poet who wrote in Urdu under the pen name Zafar.
Why some people name Akbar or Shah Alam II
Some people say Akbar was the true founder because he built the administration that lasted, and Humayun’s reign was interrupted by Sher Shah Suri. Others name Shah Alam II or Akbar II as the last “real” ruler since later emperors held little power. These are interpretations, not different facts. By the usual definition, the line runs from Babur to Bahadur Shah II. For every emperor in between, see the Mughal kings timeline.