The short answer to this is that India became independent from the British when the British voluntarily granted India its independence. In 1947, the British Parliament passed the Indian Independence Act, creating the new countries of India and Pakistan. Thus, India became independent through an act of Parliament.
The longer answer is that India became independent for at least two reasons. Perhaps the less important of these reasons was WWII. WWII badly sapped the strength of the United Kingdom. This left it without the resources it might have needed to maintain sovereignty over India. It also put the United States in position to lead the non-communist world, which mattered because the US did not generally approve of colonialism. Thus, WWII severely weakened Britain’s ability to hold India.
The more important of these reasons was the long rise of Indian resistance to being colonized. Beginning at least with the creation of the Indian National Congress in 1885, there was an organized movement to reduce British power and, eventually, to bring independence to India. The Indian elites were often educated in England and did not see any good reason that Indians should not have more power at home. They pushed strongly for greater self-government and, eventually, for independence. The most famous figure in this movement was, of course, Mahatma Gandhi. His various protest movements against the British helped to undermine the legitimacy of British rule and to gain support for Indian independence.
When you combine these two factors, you can see why Britain was forced to give India its independence. It had been under greater and greater pressure to do so from within India as the independence movement grew stronger. Because of WWII, it had less of an ability to continue to subjugate India. For these reasons, Britain “voluntarily” gave India its independence in 1947.