They are just different cases, and to be able to use the correctly, I'm afraid you have to learn the verb patterns.
In general, the accusative case is used for the direct object and the dative case for the indirect object: "Der Junge gibt dem Mädchen ein Buch".
Also, for location, one uses in most cases a preposition ("in" or "auf") and the dative case.
For a direction, it's preposition + accusative case.
"Heute gehe ich in die Schule" (in + Akk., "towards school").
"Ich habe gestern mein Buch in der Schule vergessen" (in + Dat, "in school").
But there are many exceptions, you really need to learn the appropriate case and preposition(s) together with the verb.