Pakistan was formed in 1947 as an independent state from India under the colonial rule of the British Empire, who had decided to end their rule in British India.
The British decided to partition the country in two, dividing the dominant religions of Islam and Hinduism, with the Muslim area becoming Pakistan.
You can find out more from this great video from The Economist:
The partition of India caused as many problems as it solved. The sudden division caught many people unaware and many fled their homes and land. This mass exodus caused chaos and hysteria, which in turn led to widespread violence, and faith-based massacres occurred behind country lines.
Another problem was the creation of an area known as Kashmir, which sat between the two new countries of India and Pakistan, and was claimed by both. Kashmir became the site for many conflicts, and is one of the most militarized areas in the world.