All nine two-way prepositions
An, auf, hinter, in, neben, über, unter, vor and zwischen take dative for location and accusative for destination. Ask whether the phrase tells you where something is, or where it is being placed. Stehen/stellen, liegen/legen and sitzen/setzen are useful pairs.
Understand the idea
The same spatial preposition can describe a location or a new destination. Verbs of position and placement make the contrast especially clear. Liegen versus legen and stehen versus stellen often describe corresponding states and actions.
Build it step by step
Identify what the prepositional phrase locates. If it tells where something is, use dative. If it gives the destination of a placement or change of position, use accusative. Do not confuse the accusative object being moved with the case inside the separate prepositional phrase. Both may appear in the same sentence.
See it in a sentence
Das Bild hängt an der Wand.
Ich hänge das Bild an die Wand.
A useful memory aid
Picture the before and after. If the phrase identifies a new spatial relationship at the end of the action, accusative is often the relevant choice.
A common trap
A movement verb does not by itself settle the case. Walking inside a room can be located in that room, while walking into it describes a destination.