The Dativ is the third case. Where the Akkusativ marked the direct object (what is acted on), the Dativ marks the indirect object — the recipient, the beneficiary, the person something is done for or given to. It also appears after a fixed set of prepositions.
The Dativ marks the indirect object — the person or thing that receives the direct object. The simplest test: ask "to whom?" or "for whom?" after the verb. That answer takes the Dativ.
Notice: the sentence has three noun roles at once. "Ich" is the subject (Nominativ). "Einen Kaffee" is what is given (Akkusativ). "Dem Mann" is the recipient — to whom it is given (Dativ). German handles all three with different articles.