It’s a pretty complicated question in that it is answered easily. But understanding the actions of the people involved is the hard task. I will attempt an answer.
When the British Raj was divided into India and Pakistan upon independence, those were not the only two nations that came into existence. The 560+ princely states were given the choice to join either nation or become independent. In general, this was not a real choice. Princely states landlocked deep within India would not have been viable and their accession to India was quite smooth. Union home minister Sardar Patel was instrumental in getting these states on board with the accession - and is called Bismarck of India sometimes for that.
The problems arose with three princely states. Junagadh and Hyderabad that wanted to join Pakistan ( these states had sea route but were surrounded by India and had a population that wanted to join India ) and JK ( jammu Kashmir as its referred to henceforth ) that wanted independence.
The former two are interesting in their own right and the fibers that form fabric of that history appear elsewhere too but it’s something I can answer in follow up if there’s interest.
To come back to JK, the situation became complicated as part of the population and militants from across the border rose up in rebellion and wanted the state to accede to Pakistan. Under this duress, the king acceded to India. ( this much is heavily contested - as in whether popular support existed for joining Pakistan etc and that’s a whole different story. Suffice it to say that when instrument of accession was signed a part of the state was now in control of India and another under Pakistan )
Now, this is a legal document but it had to be ratified by the states constituent assembly. In order to convince the state to do so, article 370 was added to the constitution as a temporary measure declaring JK as a special state until it could arrange the terms of accession to the union of India. The constituent assembly was to recommend the abrogation of the article or amendment upon the conclusion of its duties but complications further arose when the assembly dissolved itself without doing so, thus making the measure permanent.
This is where it stands. It’s going to need a constitutional amendment to remove or Supreme Court judgement deeming it unconstitutional( less likely as it can be overturned by the parliament - as had been done in the past about a different hot Button issue )