Generally speaking, both terms can refer to the Spanish language as a whole, with a preference for one term over the other that depends on the context or the speaker's origin; Castilian (castellano) has another somehow more restricted meaning, relating either to the old romance language spoken in the Kingdom of Castile in the Middle Ages, predecessor of the modern Spanish language, or to the Spanish dialect nowadays spoken in the historical region of Castile, in central Spain.
Originally Castilian (castellano) referred to the language of the Kingdom of Castile, one of several northern kingdoms that spread across the Iberian Peninsula through the Middle Ages, from the about the eighth to the fifteenth centuries. The first recorded example of written Castilian/Spanish is the Glosas Emilianenses, a document from the eleventh century. This protoromanic language is no longer spoken, but can be read in many texts such as El cantar del Mio Cid. This language derived from Latin and evolved into the modern Spanish, or Castillian, language.
However, the term Spanish (español) is a more recent term that first referred to Spain as a country, and then to the predominant language spoken in that country. Spain as a truly unified nation appeared centuries later than the language and the Kingdom of Castile; in fact, it was only in 1469 that the marital union between the Kingdoms of Castile and Aragon gave way to the modern Kingdom of Spain. The actual legal unification date is disputed, but commonly agreed to have not occurred earlier than the eighteenth century. Only then did the Castilian language begin to be commonly called Spanish.
In 1492, the arrival of Christopher Columbus on a Castilian-paid expedition paved the way for the Spanish colonization of the Americas. As a result of this process, many countries in South America now speak the same language as Spain. But until about the eighteenth century, it was the Kingdom of Castile, and not Spain as a whole, the colonization power; so the language they used was called Castilian. Thus, some American countries formerly under the Castilian/Spanish rule have retained the custom of calling it el castellano, while others switched to calling it el español at some point or the other, with many other different factors influencing the final choice.
In English, the term Spanish relates more to the language than to the nation; the noun and adjective used today for people from Spain is Spaniard. Also, the use of the term Castilian is much less widespread than the use of Spanish.
To understand how two terms can refer to the same language, imagine that the English language was sometimes called English after the historical nation whose language it is, but also sometimes British after the modern state, the United Kingdom of Great Britain and Northern Ireland, of which it is the official language. To add to the complexity, former British colonies such as British North America had to choose a name for the language, as did the speakers of Welsh and other non-English languages in the United Kingdom. This resembles the situation with Spain and its historical centre, Castile.
Castile (in Spanish: Castilla) means Castle-land, from castiello plus the suffix -ia, giving Castiella, a form that survives in the Astur-Leonese language and can be seen in mediaeval Castilian texts such as the Lay of the Cid. Modern Spanish has transformed all words ending in -iello, -iella into -illo, -illa. The adjective derived from Castilla is castellano, or 'Castilian', in English. Castellano also means 'castellan', i.e. a castle master. There is a comic scene based on the play on words (Castilian/castellan) in Don Quijote.
The term español, which was adopted by several languages — and transformed according to their own phonology and grammar rules — to designate the Spanish people and their language, is from medieval Latin Hispaniolus (literally, "little Hispanian"), a form that evolved later, by hypercorrection, to the written form Spaniolus (note that, by that time, the Latin h had become silent, causing the word to be pronounced [ispaˈniolu]), and the prosthetic vowel [i] (used in spoken Latin for euphonic reasons) opened to [e], giving the present word.
As the branches of Vulgar Latin began to evolve into separate Romance languages, the term that would evolve into español began to be used to refer to these derivative languages (especially as opposed to the Arabic and Hebrew of the Moorish and Jewish inhabitants of Iberia). It was at first a general term that embraced the various dialects of Iberian Romance spoken in the area, including the forebears of modern Galician, Castilian and Catalan. However, with the rise of Castile as a power, and its absorption of all surrounding regions into an ever-growing empire that eventually spread to the New World, the term España was eventually equated with the peninsular territories ruled by the Crown. With this, the break with the Roman concept of Hispania was complete, and the term acquired its modern meaning of 'all of Iberia except for Portugal and Andorra'. Similarly, el español came to be used to refer to the common language of this new country: Castilian.
The terms España and español spread to other languages. The English name 'Spain' is from the French Espagne. 'Spanish' is 'Spain' plus the English suffix -ish. The term continues evolving as other languages adapt these words to form their own name for Spain — for example, Japanese スペイン語 (Supein-go), 'Spanish language', and スペイン人 (Supein-jin), 'Spaniard', derives from the Japanese word for Spain, スペイン (Supein), which, in turn, derives from English 'Spain'. In Chinese though, the word is directly taken from Spanish (or perhaps even Latin) rather than English: they say 西班牙 (Pinyin phonetic symbols: xībānyá) for Spain and 西班牙语 (Pinyin: xībānyá yǔ) , or the abbreviation 西語 (Pinyin: xī yǔ) for the Spanish language. The Arabic إسبانية (isbaniya) for Spain derives directly from the word España: the absence of "p" in the Arabic alphabet makes it a "b", and the sound "ñ" is transformed into "ny". إسباني (isbani) is the name for Spanish, with the same end of عربي ('arabi that means "arabic").
While Espanyol is used in Tagalog and other languages of the Philippines, the word Kastila is more frequently used. Furthermore Katsila is also used among those who speak Visayan languages like Cebuano.
In Guatemala, although Spanish is the official language, the Mayans, the original inhabitants of the region, call it "la castilla", keeping the original name from colonial times. Mayans speak at least 22 different "Mayan" languages and dialects (Mam, Pocomam, cak'chikel, tzu'tuhil, kek'chi, ki'che, etc).
* denotes an unattested or hypothetical form.
The expression Hábleme en cristiano "talk to me in Christian", uttered to people not speaking Spanish at the moment, is used in opposition of the other Spanish languages, which is felt as annoying by them (Catalonia, Galicia and the Basque Country, but not in America).
There do exist many other Academies (reunited at the Association of Spanish Language Academies) that may or may not have an official normative recognition, but nevertheless cooperate in the elaboration of the Diccionario Panhispánico de Dudas (a compendium of corrected typical mistakes and doubts). In this dictionary, whose redaction was agreed upon by the 22 different Spanish Language Academies, we may read:
Thus, even if both terms are allowed in Spanish, the usage of el español is recommended for the language as a whole. However, popular choice of terms is not so clear, with other factors such as customs or politics coming in.
This choice of words can however vary depending on many factors, including the origin of the speaker or some political nuances.
Monolingual regions outside of Castile include, mainly, Andalusia, but also other regions where the regional languages are not developed enough to be widely spoken by the majority of the population; this is the case of Extremadura, Cantabria or Aragon, for example. There, español may be used as in Castile to stress the national nature of the language, but with a slightly different nuance: they are accepting another region's historical language as their own. However, one must not forget that if Castilian is spoken in these regions it is due to the Reconquista, and thus it was not imposed as happened in other regions such as the Basque Country or Catalonia.
For some, this use of the term castellano or Castilian is a political or cultural statement that Castilian is only the language of Castile and perhaps some areas that Castile colonised, but not the language of their region, where they consider the only legitimate language to be the regional one, i.e. Catalan, Basque, etc. This stance is common in regionalist circles.
Conversely, some nationalist circles prefer the term español because they perceive their ethnic community to be distinct from that of Spain, and therefore do not object to the language of Spain being called Spanish. In Basque-speaking regions, where the language is not of Romance origin — Basque is considered by many scholars to be a language isolate — some Basque speakers might even use the term erdara (lit. foreign) specifically for Spanish, since for them it is the prevalent foreign language - just as in the French Basque Country, "French language" is the usual meaning of erdara.
However, some Latin Americans prefer the word castellano, especially in Argentina, Chile, and Peru. One reason for this is that many of the early Argentine settlers were Galicians, so castellano had the special connotation of standard Spanish as opposed to Galician.. Reasons given generally include the idea that Spanish is an international language with historical origins in the old kingdom of Castile, and that the term español is imperialist, implying it is the language of Spain. One criticism of this reasoning is that Castile is the imperialist heart of Spain, and the engine that drove the colonization of the Americas, so castellano is just as 'bad' in these terms as español. However, the fact that Spain is still a major nation-state, whereas Castile is now a region buried within Spain and internationally forgotten, is the deciding psychological factor.
In practice, the use of one term or the other tends to be a matter of local customs, rather than reflecting any philosophical or political ideas.
However, in some Latin American nations, castellano may be used to specifically describe the variation(s) of the language spoken in the castellano speaking regions of Spain, while español would generally refer to Standard Spanish.
Some constitutions avoid the issue by talking about "the national language".
Some philologists use "Castilian" only when speaking of the language spoken in Castile during the Middle Ages, stating that it is preferable to use "Spanish" for its modern form. The dialect of Spanish spoken in northern parts of modern Castile may also be called "Castilian." This dialect differs from those of other regions of Spain (Andalusia or Madrid for example); the Castilian dialect is conventionally considered in Spain to be the same as standard Spanish.
Another use of Castilian in English is to distinguish between standard Spanish and regional dialects. As noted above, this distinction is made to some extent in Spanish, but not as far as some English speakers go — for example, websites with language selection screens giving the choice between Castilian Spanish and Latin American Spanish among other languages.
In the Americas, where Spanish is the native language of 20 countries, usage of castellano and español is sometimes reversed when referring to another nation. For example, a Peruvian, talking about a Uruguayan, might say Yo hablo en español peruano, él habla en español uruguayo, pero los dos hablamos castellano ("I speak Peruvian Spanish, he speaks Uruguayan Spanish, but we both speak Castilian"). This odd usage comes from the historical association of español with the language that was brought to America by conquistadores, and later transformed in each nation through daily usage, and castellano as the basis for all variants.