In this sentence »den« is an article (like »the« in English):
Auf dem ersten Foto sieht man den lustigen Busfahrer, ...
On the first photo you see the funny/happy bus driver, ...
and »der« is a relative pronoun (like »who« in English):
(Man sieht einen Busfahrer), der manchmal eine Haltestelle vergisst.
(You see a bus driver), who sometimes forgets a bus stop.
German has two grammatical numbers (singular and plural), in singular it has three grammatical genders (male, female and neuter) and it has four grammatical cases (nominative, genitive, dative and accusative case), and for articles and adjectives it also has two determinations (definite and indefinite).
This four characteristics (number, gender, case, determination) together define the declension of some parts of speech. Articles are such a part of speech, that must be declined according to all four characteristics.
You will find declension tables for articles in your German book.
- Here in this example we have one bus driver, so the grammatical number is singular.
- The word »Busfahrer« is a compound noun, its parts are »Bus« and »Fahrer«, and the gender of a compound noun is always the gender of the last part, i.e »Fahrer« in this case. In German, the grammatical gender of »Fahrer« is male. (It's always the noun itself that defines the grammatical gender. Other parts of speech, that are connected to this noun inherit the gender from the noun.)
- You don't see just any/some bus driver, but you see a very special bus driver. It's the one who is funny or happy (both is »lustig« in German) and who sometimes forgets to stop. So the bus driver is well defined, and so the determination is »definite« (it's not indefinite).
- The verb »sehen« (used in the inflected form »sieht«) needs an object in accusative case (its the verb that defines the grammatical cases of it's objects), and this object is »der lustige Busfahrer« (consists of: article, attributive adjective, noun). Since we don't need nominative case, but accusative case, we have to decline all words that make up the accusative object. So all three words must be transformed. (»Leave as it is« is also a possible transformation!)
So we have all together: Singular, male, definite and accusative case. This gives the article »den«.
This also influences the way how the adjective has to be declined:
»Lustig« in singular, male, define mode and accusative case is »lustige«.
Plus the noun also has to be declined. It's the noun itself, that defines the grammatical gender, so you don't really decline nouns by it's gender. Also determination doesn't influence the way how nouns are declined (it only influences articles and attributive adjectives). So nouns are only declined by grammatical case and number.
But singular already is the standard form, and in case of »Busfahrer« accusative case looks exactly the same as nominative case, so nothing must be done to transform »Busfahrer« into singular and accusative case.
Relativpronomen (relative pronouns)
A relative pronoun is a pronoun that marks the beginning of a relative clause. A relative clause is something like a sentence within a sentence. The relative clause gives an explanation or description of something that stands in the same sentence, but outside the relative clause.
In the given example we have this relative clause:
..., der manchmal eine Haltestelle vergisst.
..., who sometimes forgets a bus stop.
This is a description of someone who was named before in the main sentence.
A pronoun is also a placeholder for a noun. It refers to a noun. In this example it is a placeholder for »Busfahrer«, and so the whole relative cause is a description of that bus driver. This is how relative clauses work in German as well as in English.
Since »Busfahrer« is a male noun, the pronoun which refers to is, must also be male. Gender must always match when using pronouns. The noun defines the gender, and the pronoun must be declined according to that gender. Also the number (singular or plural) must match. (German has genders only in singular, so when it's plural, you can stop worrying about genders.)
The grammatical case doesn't need to be matched between pronoun and referred noun. The grammatical case of a relative pronoun is defined by the verb in the relative clause. In your example we find, that the pronoun is the subject of the relative clause, and therefore has to be used in nominative case. (The subject is the most important object of the verb. It's so important, that it's no longer called »object« but »subject«, but is still is ruled by the verb. In almost all sentences the verb has such an subject, and a subject is always used in nominative case.)
German has this relative pronouns:
- »Der« which becomes »die, das, dem, den, denen, dessen, deren, derer« when declined by number, gender and grammatical case.
- »Welcher« which also appears as »welche, welches, welchen, welchem« when declined.
- »Wer« (»wes, wessen, wem, wen«), used for persons and personalized entities (pets for example) only.
- »Was« (»wes, wessen«), for non-personalized entities
Look up your German grammar book for detailed declension tables of relative pronouns.