General phonetic "rules"
For a lot of words that are now written using Katakana, there was a tiny amount of logic to it. 亜 transcribed ア virtually exclusively, 伊 is a very common kanji for transcribing イ, 加 for カ, 利 for リ, and generally the kanji from which Katakana come is a good rule, though far from perfect (as noted with ア, and in タ which is usually transcribed as 太 (origin of た)) [see image]
In fact, sometimes there aren't even consistent transcriptions for one word. アラビア on my IME gives options of 亜拉毘亜 and 亜剌比亜, and even 阿剌比亞 (using the older version of 亜). Nowadays katakana is standard. The website in the comments section of the main post has a much larger list.
Syllables ending with -n tended to be transcribed by syllable (as opposed to kana). 芬蘭 (フィンランド) is one example of this.
Chinese's Influence
Sometimes the transcription was a derivation of the hanzi used to transcribe something in China. スペイン (西班牙) would by conventional logic be read as "seibanga", with the most common respective readings of the characters being せい、はん、and が. In fact, the origin comes from Chinese, where the word is read as "Xibanya". Much closer to the pronunciation of "España" we're used to! The Japanese word is スペイン, obviously a loan from English, but doesn't match these characters much.
Meaning
Sometimes a fair few characters are possibilities, and the one chosen doesn't follow any pattern above. One reason for this was the attempt to still keep Kanji with some semantic (meaning) aspect to the word. フランス、スペイン are both examples of this, with respective characters both containing 西(仏蘭西、西班牙). This makes the actual transcriptions generally quite messy.
Other notes
There are non-country names that got given kanji to transcribe sounds too, including 缶{かん}, which was obsolete by the time the Dutch arrived in Japan, so was revived to mean "can". 亜細亜 is common for "Asia" on wartime post stamps. 煙草{たばこ} is common, even today, in place of katakana for "tobacco" (both chosen because of meaning purely, and have 0 phonetic relation to the word).
It's not common to write out entire words like this anymore, with katakana largely replacing it. You'll see it every now and again, but much more commonly these characters are still in frequent use in newspapers as contractions of country names to keep headlines short. Headlines such as
独、伊決勝へ!"Germany and Italy advance to the finals" (say, in soccer)
米国大統領来日 "American President Comes to Japan" (all kanji)
are in the newspaper I have in front of me right now, for example.