There is a rather interesting historical review at Le Monde Diplomatique (in English) on how the then Spanish provinces of Saguía el Hamra and Río de Oro (then legally as much part of Spain as Madrid, by the way - so good for the unalienable unity of the fatherland) ended up in Moroccan hands with the blessings of the USA and France.
Map of the colonial West Sahara and Morocco in the mid 20th century (from Wikipedia).
Apparently the USA saw Algerian interest in supporting the Sahrawis as part of the Cold War geopolitics, so, by default, it supported Morocco discretely. Spain was reluctant to yield the territory and specially if it appeared they had been kicked out, however it eventually yielded to pressure in the context of an uncertain regime change.
The appointed Sahrawi council (Yemaa or D'jemaa) however declined to play the rubber-stamping role that the USA, France, Morocco and Mauritania had devised for them and instead declared its own dissolution and transfered its power to the recently created POLISARIO Front, which proclaimed the Sahrawi Arab Democratic Republic.
To Moroccan disillusionment the African Union Organization, as well as most African states, also recognized the rebel republic and the weakest part, Mauritania, soon yielded to Polisario attacks and recognized the country's independence in 1979. Morocco became isolated in Africa. However, later on, the Moroccan tyranny (it's a police state where people fear speaking freely, I can tell you), with massive support from Saudi Arabia, the USA and France, managed to build their version of the Great Wall, forcing the Polisario out of most of the country and preventing them from attacking the much coveted phosphate mines and foreign fishing ships that had no authorization from the Republic.
All this only barely dealt with in the Le Monde Diplomatique article but it is still interesting to read to understand how the imperialist interests of the USA and France played in favor of the last absolute monarchy of Africa... and to read how Kissinger view the issue on his own words.
However, in spite of a two decades long cease fire, the wound is still open and the war might start again at any moment. Moroccan official maps, of the kind you find only there, have no borders south of Oujda, as the fascist monarchy claims all the desert for them: a good bunch of Algeria, all Mauritania and, who knows, maybe even Mali.
Further reading (in Spanish) at Sahara Libre, where you can find news of the ongoing conflict such as the violent repression of striking workers yesterday, the long hunger strike of Dr. Abbas Mohammed Chej Sebaj or how lobbyists in the USA are trying to bring the now unemployed Louisiana fishermen to the rich Sahrawi banks.