It was a Dutch colony right? So how did it end up under British control during WW2?
Because it had been British for 250 years by the time WW2 started.
During the French Revolutionary Wars, the French invaded the Netherlands and set up a puppet govt essentially, that gave Britain all the excuse it needed to gobble up such a valuable location. In the horse trading and treaties towards the end of the Napoleonic wars in 1814-1815 Britain kept Cape Town and the colony.
However increasing British immigration and of course preferential treatment and government did not sit great with the existing Dutch colonials, thus many of them moved towards the interior of Africa far from the immediate centers of British power.
Over the decades many of the settlers of Dutch decent, known as the Boers managed to form independent states, that received international recognition and even acquiescence from Britain. Thus the land to the North of the Vaal River or the Transvaal, became home to several Boer Republics. The most powerful being the South African Republic, or just colloquially the Transvaal, and the Orange Free State.
However with the expansion of the British Cape Colony inwards conflict was inevitable in the face of growing Imperial ambitions, just as the native states found out, the Boers would too, meaning a constant series of tension provoking incidents and isolated instances of violence. The discovery of diamonds set of a rush of new groups and interest in the area most famously at British controlled Kimberley(despite it being on the Boer side of the river) where a young Cecil Rhodes would be involved in snapping up several previously Boer owned mines that would be consolidated into the modern De Beers company.
In 1880 tensions boiled over into war but after initial Boer victories the Gladstone government in London balked at getting involved in a quagmire and a peace deal was struck. In 1899 though after an additional Gold Rush in the Transvaal, and even an attempt to incite a revolt by non Boers sponsored by Rhodes, war broke out again. It dragged on until 1902 but eventually the Boers were forced to the table, and with comparatively easy terms the whole of South Africa was finally brought under British control.