When Bolesław III died in 1138, his will split the country between his five sons. His eldest son was to be the High Duke, theoretically above the others in power, ruling the area around Kraków. This division of the state led to a period of instability that lasted well over a century. The country began to become united again around 1300 under foreign rulers, the Czech Premyslids.
The Piasts soon returned to power, however their dynasty ended after the reign of Kazimierz III the Great. Kazimierz had no male children; he was succeeded by his nephew Louis (Ludwik) I of Hungary. Louis was succeeded by his daughter Jadwiga and her husband Jagiełło in 1382, thus ushering in the Jagiellon dynasty.
Through Jagiełło, the Jagiellons were also the hereditary Grand Dukes of Lithuania. In 1569 the two states were officially united as the Polish-Lithuanian Commonwealth. After this time the kings were elected by the nobles.
The Commonwealth became weak in the 18th century and finally came to an end when it was divided between the Russian Empire, Prussia, and the Habsburg Empire. Poland would not be an independent country again until 1918, at which time it became a republic.