Tokugawa and his descendents ruled over a peaceful Japan for two and half centuries. The role of the samurai in peacetime declined gradually over this period, but two factors led to the end of samurai: the urbanization of Japan, and the end of isolationism.
As more and more Japanese moved to the cities, there were fewer farmers producing the rice needed to feed the growing population. The luxurious living enjoyed by the shoguns and many daimyos started to wear away at the economic system. Many Japanese, including lower class samurai, grew dissatisfied with the shogunate because of the worsening economic conditions.
Then, in 1853, U.S. ships sailed into Edo Bay. Commodore Matthew Perry had arrived to deliver a message from President Millard Fillmore to the emperor (who still existed as a figurehead, even though the shogun really ruled the country). Fillmore wanted to open trade relations with Japan, wanted shipwrecked U.S. sailors to be treated well by the Japanese, and wanted to open Japan as a resupply port for American ships. Perry delivered his message, told the Japanese he would return after a few months, and left.
In Perry's wake, a split grew in Japan. Some wanted to deny the American offer, maintain isolationism, and stay with their ancient traditions. Others, however, realized that Japan could never stand up to the better technology of the westerners. They proposed opening up Japan to learn everything they could from the Americans, ending isolationism and becoming a world power. Ultimately, the bakufu decided to open Japanese ports for American resupply, and later to trade.
The emperor refused to agree to the treaty. Because he was just a figurehead, the bakufu went ahead with the treaty anyway. Several groups of rebellious samurai, who wanted Japan to stay the same, supported the emperor and began a civil war against the bakufu. Surprisingly, they overthrew the shogun, ending the Tokugawa period and restoring the emperor to power. Lower class samurai took positions of leadership, controlling the government from behind the new emperor, a young boy who was called Emperor Meiji. This event is known as the Meiji Restoration.
The power of the daimyos was taken away as the government seized their land. With no one to pay the many samurai, the government decided to pay them with bonds based on their rank. This affected low- and high-ranking samurai differently, but had the same result -- each class either used the bond to invest in land or start a business, or realized they didn't have enough income to support themselves, and returned to the land as farmers or to the cities as workers. The samurai no longer had a role in Japan.
Finally, in 1876, the emperor banned samurai from wearing their swords, leading to the creation of a drafted standing army. The final bell had tolled for the samurai -- they no longer existed. Though there were some rebellions as samurai in outlying provinces resisted, all the samurai eventually adopted new roles in Japanese society, as their nation moved into the Industrial Age.