The rule for this is simple: in the nominative case with the definite article (the/der, die, das) the adjective ending is -e when the adjective comes before the noun. So we would get "Der blaue Wagen..." (The blue car...), "Die kleine Stadt.." (The small town...), or "Das schöne Mädchen..." (The pretty girl...).
But if we say "Das Mädchen ist schön." (The girl is pretty.) or "Der Wagen ist blau." (The car is blue.), there is no ending at all on the adjective (schön or blau) because the adjective is located after the noun (predicate adjective).
The rule for adjectives with the definite article (der, die, das) or the so-called der-words (dieser,jeder, etc.) is simple, because the ending is always -e in the nominative case (except for the plural which is always -en in all situations!).
However, when the adjective is used with an ein-word (ein, dein, keine, etc.), the adjective must reflect the gender of the noun that follows. The adjective endings -er, -e, and -es correspond to the articles der, die, and das respectively (masc., fem., and neuter). Once you notice the parallel and the agreement of the letters r, e, s with der, die, das, it becomes less complicated than it may seem at first.
The endings in the ACCUSATIVE (direct object) case are identical to those in the NOMINATIVE (subject) case, with the sole exception of the masculine gender (der/den). The masculine gender is the only one that looks any different when the case changes from nominative (der) to accusative (den).
In the sentence "Der blaue Wagen ist neu," the subject is der Wagen and der Wagen is nominative. But if we say "Ich kaufe den blauen Wagen." ("I'm buying the blue car."), then "der Wagen" changes to "den Wagen" as the accusative object. The adjective ending rule here is: in the accusative case with the definite article (the/den, die, das) the adjective ending is always -en for the masculine (den) form. But it remains -e for die or das. So we would get "...den blauen Wagen..." (...the blue car...), but "...die blaue Tür.." (the blue door), or "...das blaue Buch..." (the blue book).
When the adjective is used with an ein-word (einen, dein, keine, etc.), the accusative adjective ending must reflect the gender and case of the noun that follows. The adjective endings -en, -e, and -es correspond to the articles den, die, and das respectively (masc., fem., and neuter). Once you notice the parallel and the agreement of the letters n, e, s with den, die, das, it makes the process a little clearer.
Many German learners find the DATIVE (indirect object) case to be intimidating, but when it comes to adjective endings in the dative, it couldn't be more simple. The ending is ALWAYS -en! That's it! And this simple rule applies to adjectives used with either the definite or indefinte articles (and ein-words).
In genitive phrases, the adjective ending is (almost) always -en, as in des roten Autos (of the red car), meiner teuren Karten (of my expensive tickets) or dieses neuen Theaters (of the new theater). This adjective-ending rule applies to any gender and the plural in the genitive, with almost any form of the definite or indefinite article, plus dieser-words. The very few exceptions are usually adjectives that are normally not declined at all (some colors, cities): der Frankfurter Börse (of the Frankfurt stock exchange). The genitive -en adjective ending is the same as in the dative case. This applies even to genitive phrases without an article: schweren Herzens (with a heavy heart).
Memorize the following tables:
Table 1: German adjektiv endings with the defined article (der, die, das ...)
Table 2: German adjektiv endings with the undefined article "ein"(ein, dein, keine, etc.)
Table 3: German adjektiv endings without article