Names of persons
This answer explains that there are semi-official, conventional methods of transliterating foreign names to Chinese, and sometimes they are not the closest to the English pronunciation, even for English names. According to this answer, transliterations are sometimes done for different Chinese "dialects" - e.g. Mandarin or Cantonese - and, confusingly, sometimes they get mixed up, e.g. a Cantonese transliteration is pronounced using Mandarin, yielding an even more inaccurate pronunciation. For example, Taxi was transliterated using Cantonese as 的士 (Dīksíh) which is pronounced in Mandarin as di1 shi4.
Names of places
For place names, transliteration often uses the native pronunciation of the place name. This may not be obvious, but English transliterations are sometimes inaccurate too, for various reasons. For example, this question asks why Germany was translated as 德国 (de2 guo2) (the answer is the German name for Germany - Deutschland). Same goes for your example of Spain - 西班牙 (xi1 ban1 ya2) - España. For a truly confusing example, see how Japan's English and Japanese names have the same origin, but are so dissimilar: Nihon (Japanese) - Cipangu (Marco Polo, from Wu Chinese) - Jepang (Old Malay) - Giapan (via Portuguese) - Japan (English).
So in conclusion, there are reasons for the transliterations, it's not completely random, but it's very hard to learn because it's quite complex, so it may be easier to remember by rote.
Chiang Kai-shek
into常凯申
. See, such mistakes can be made even by a professor of history!