Chinese merchants had been trading with Taiwan during the Ming, and Chinese merchants settled there, either for the short term, or for longer stays. During the late Ming, the volume of trade grew, and Chinese settlement increased. Chinese pirates found Taiwan a convenient base, free of the authority of a centralised state. The next players to enter the scene were European colonial powers, with the Dutch and the Spanish establishing bases. The Dutch drove the Spanish out.
The Dutch were an important influence in local pirate politics, and partly due to Dutch support (and despite some violent conflict with them), Zheng Zhilong became the dominant pirate in the region (Fujian and Taiwan). In 1628, he received an official appointment from the Ming, as an anti-pirate. In this capacity, he fought the Dutch at sea. He also continued to be a major legitimate trader.
Then the Ming Dynasty fell. A rebel leader captured Beijing, and proclaimed himself emperor, and the last Ming emperor committed suicide. The Ming army guarding the Great Wall against the Manchus (who had already proclaimed themselves as emperors of China) defected to the Manchus, and together they captured Beijing from the rebels. This was 1644, and Qing rule of China had begun.
The Ming were not finished. New Ming emperors were crowned, and continued to resist the Qing until 1662. Zheng Zhilong was part of this resistance at first, but surrendered to the Qing in 1646. His son, Zheng Chenggong (AKA Koxinga) continued the struggle. Like his father, he was a seafarer, both pirate and trader, and took over his father's maritime kingdom. Losing his bases on the mainland in Fujian, he moved to offshore islands. The closer islands proved to be vulnerable to the Qing, and he looked for a better, more secure, base. Taiwan beckoned. He gathered his fleet and army, and conquered Taiwan from the Dutch in 1661. Taiwan would be the base, he planned, from which the Chinese mainland would be reconquered - his retreat to Taiwan has parallels with the Nationalist retreat to Taiwan following their defeat on the mainland in 1949, in the Chinese Civil War.
Zheng Chenggong did not rule Taiwan for long. Shortly after his conquest of Taiwan, he died of malaria, and his son took over as the new ruler. The Qing were not happy with an offshore Ming loyalist base (even after the total defeat of the Ming on the mainland), and in 1683, the Qing emperor Kangxi moved to conquer Taiwan from the Zhengs. The Qing fleet won a major naval battle (the Battle of Penghu (1983), with over 200 ships per side, and 20-30,000 men on each side), and Taiwan lay vulnerable. The ruler of Taiwan, Zheng Keshuang, a grandson of Zheng Chenggong, surrendered to the Qing, and was given a hereditary noble title, and retired to Beijing.
Taiwan would be ruled by the Qing until its loss to the Japanese as a result of the Sino-Japanese War in 1895.
Further reading:
An excellent book on this topic, Tonio Andrade, How Taiwan became Chinese, Columbia University Press, 2008, has been placed into the public domain by the publisher, and is freely available online: