This rule is true for words of Greek origin ending with "-ma." However, words that are not of Greek origin are typically female (e.g. the llama / la llama, the dame / la dama).
The reason for this is that in Greek these words were originally neuter, and they remained neuter when they were absorbed into Latin. However, as Spanish diverged from Latin, those neuter words became masculine.
Briefly, idioma is used when talking about a certain language, like the language of a country or of a specific group of people. Spanish language, the language of the court, etc.
Lengua/lenguaje is used when talking about how someone expresses themselves, or when talking about speech itself. Foul language, colloquial language, the likes.
The definite article in Spanish is used whenever you're referring to something specific. That's pretty much it.
The biggest difference to English is probably how generalisations are handled. English doesn't use articles when you make a generalisation (i.e. claim that some property applies to every instance of some noun), but Spanish does:
- Snow is white. - La nieve es blanca.
- I hate apples. - Odio las manzanas.
- Children don't pay taxes. - Los niños no pagan los impuestos.
Definite articles are used with names of languages in Spanish when the language is the subject of a sentence.
El japonés y el alemán son lenguas difíciles. Japanese and German are difficult languages.
El español me gusta mucho. I really like Spanish.
When a language is the object of a verb, the definite article is not used.
Yo hablo español. I speak Spanish.
Quiero aprender español. I want to learn Spanish.
Me gusta estudiar español. I like to study Spanish.
"Idiom" does not precisely mean "language".
Idioma is slightly different from lengua, but colloquially they get interchanged quite a bit. Idioma refers to the language of a particular people or country: "El español es el idioma de muchos países de América del Sur." Lengua is a bit broader, referring to that systematic communication form in general: "Actualmente estoy aprendiendo cuatro lenguas." I would use lenguas in the above sentence.
"Proper nouns" are names of things and people, and you usually don't give them articles, like "George" (Jorge), "Spain" (España), or "Walmart" (Walmart).
The español here is a nominalised adjective, i.e. an adjective that you're using as a noun to represent some concept. Nominalised words (including other types like verbs or numerals) are always masculine in Spanish.
EDIT: Slight correction here. Nominalised adjectives are usually neutral and use the neutral article lo, like in "Espero lo mejor" - "I hope for the best." The español in this sentence is an actual masculine noun, referring to the Spanish language. (Sep. '19)
I explained that above. In short, idioma refers to what you'd usually call a "language", the form of communication that a specifc group uses to exchange ideas. It's usually bound to that "group" idea, so idioma would be used for the language of a country or the language of professionals, scientists, lawyers, the likes.
Lengua and lenguaje refer to the way of speaking, and to talk about communcation itself. Lenguaje is what you'd use to talk about colloquial language, dialects, and the like.
When this happens, please reproduce your answer in the forum, using a cut/paste or screenshot if possible, so we can see what you actually wrote. It is likely you have a small error somewhere that escaped your notice. I have never, ever, had an "identical" answer marked wrong. I have often thought my answer was identical, but eventually, I have always found my mistake.