The British captured Gibraltar from Spain in 1704, as part of the War of the Spanish Succession. The status of Gibraltar as a permanent British territory was confirmed by the Treaty of Utrecht of 1713 (there have been many Treaties of Utrecht). Despite various attempts over the years by Spain to reclaim the territory by treaty or main force, it's still a British Overseas Territory with a permanent British population, though its military significance has declined over the years. A 1967 referendum on Gibraltar's status resulted in 99.96% of residents voting to remain a British possession, and a 2002 referendum had similar results though it's beyond our 20-year rule for discussion.
The reasons why the British wanted control of Gibraltar are pretty quickly ascertained by looking at a map -- the Rock is a highly defensible location as long as it can be resupplied by sea, and a naval base there commands the Straits of Gibraltar, an extraordinarily important strategic chokepoint, especially once Britain gained further bases in the Mediterranean and when the Suez Canal meant the route to India ran through the Med.