Malaysia as a country in its present shape, with 11 states and the Federal Territory of Kuala Lumpur on the Malaysian Peninsula, and the states of Sarawak and Sabah on the north of the island of Borneo, was formed in August 1965, when Singapore left the Federation of Malaysia to become an independent country. A series of momentous events actually took place in the several years prior to 1965 which finally resulted in the present political entity.
Following the Second World War and the rise of nationalism around the world, there was a strong movement in Malaysia to press for independence from Britain. in August 1957, the Federation of Malaya became independent. Then in 1961, the government proposed formation of a new political entity called Malaysia which would incorporate the Federation, Singapore, Sarawak, Sabah and Brunei. Brunei decided not to join, and Indonesia and the Philippines opposed the inclusion of Sawarak and Sabah in the new entity.
All the while between 1948 and 1960, the governments of the day had to contend with a communist insurgency which resulted in assassinations especially of foreigners and intimidations of the rural populatio. A State of Emergency was declared to handle the insurgency, which was finally ended in 1960.
In any event, Malaysia was formed in September 1963, which led to confrontation by force from Indonesia and severance of ties by the Philippines government. Meanwhile, Singapore left Malaysia in August 1965 because of political disagreements. Finally, in 1966 Indonesia and the Philippines dropped their claims on Sarawak and Sabah.
Further back in time, the Portuguese came to Malacca in the early 1500s to try to establish a trade port, but was rebuffed by the local ruler. They returned in 1511 with a fleet and this time succeeded in driving out the Malaccan ruler and establishing Portugese rule. Later came the Dutch; they had established trading posts in parts of present-day Indonesia.. In 1580, Spanish King Philip II invaded Portugal and took over the country, which weakened Portugese hold over its overseas territories. Since then, the Dutch had designs on Portugese outposts in Asia, and Malacca was a target as it was along the Europe-Asia trade route. So, in mid-1640, the Dutch attacked Malacca and after six months, the Portuguese garrison subcumbed.
The British was a late comer, but proved more ambitious and successful. Before and during the Portugese and Dutch times, the Malayan peninsula was made up of several states, each with a local ruler. The British had been looking for a port somewhere in the region to supply and refit the East India Company ships which carried goods between China and Europe. They got their first settlement on the island of Penang in 1785. Their luck continued following the French Revolution in 1789 when the French revolutionay army invaded the Dutch. The Dutch government-in-exile decided to pass control of Malacca to the British temporarily until its war with the French was over. As event unfolded, Napoleon was defeated in 1815 and the Dutch took back Malacca but in 1824 handed the settlement to the British by treaty. Meanwhile, the British continued to extend their influence elsewhere by establishing a trading post in Singapore in 1819 and managed to have the Sultan of Johor to cede Singapore to Britain.
Elsewhere in Sarawak in 1839, a young Briton called James Brooke landed there, came to know the Sarawak ruler and in 1840 helped him put down a local uprising. For his efforts, he was given control over Sarawak. Then in 1877, the British North Borneo Company obtained sovereignty over the present-day Sabah.
Back on the peninsula, Perek and Selangor were next, when their rulers faced power struggles and unrests, which was only settled through mediation by a British appointed officer. Out of this, both states agreed to accept a Resident appointed by the British, whose advice had to be sought on most matters related to the governing of the territories. In 1887, the Pahang ruler reluctantly accepted the appointment of a British Resident, which subsequently led to a local rebellion but was put down by the British.
In 1909, Siam by treaty agreed to hand over to Britain all the power and rights over Terengganu, Kedah, Perlis and Kelantan, which all became British protectorates.
Johor was the last state to come under British control when it agreed to accept a British Resident in 1914.
So, the British successfully spread its influence gradually across the peninsula, Sawarak and Sabah, often under the guise of indirect rule or "advice" by a British Resident / agent - until the Japanese invasion and occupation between 1941 and 1945.