First of all: - 'An' is a two-way preposition in German ('Wechselpräposition'), that means it takes both accusative form (with regards to any action or move) and dative form (with regards to location – a stable position, no movement).
So generally, dative form goes with the questions where (wo?) and when (wann?), while for accusative comes the question: where to (Wohin?)
Let's work with some examples:
Was hängt an der Wand?- What's hanging on the wall?
An der Wand hängt ein Foto der Familie meiner Freundin Sarah – A family photo of my girlfriend Sarah hangs on the wall.
Jetzt hänge ich ein Foto meiner Familie an die Wand - Now I'm going to hang a photo of my family on the wall
Looking at the first two examples, you understand a family photo is hanging on the wall. There is no related action or movement visible here, rather comes the question 'where' is the photo hanging? Answer is – on the wall. So 'an' takes dative form – an der Wand.
But in the third line, you are going to hang your family photo on the wall. So the question is 'wohin' (where to) - on the wall. It's an action or move, so it takes an accusative form – an die Wand.
Look at these examples:
Um mein Ziel zu erreichen, bin ich bereit bis an die Grenzen meiner Kraft zu gehen - In order to achieve my goal, I am ready to go to the limits of my strength.
So it's an action. You are ready to go to the limit or boundary of your strength (die Grenze – border). So here again, you are dealing with an accusative form of 'an' – an die Grenze.
What's the following example tells us:
Diese kleine Stadt liegt in Österreich an der Grenze zu Deutschland - This small town is located in Austria on the border with Germany.
Here, 'an' takes dative form. The small town is located in Austria, but on the broder with Germany. So it's a location-based question 'where', answer is 'on the border' - an der Grenze.
Here are some more examples (dative form) of 'an':
Erinnerst du Dich, an diesem Ort haben wir uns vor 10 Jahren kennengelernt - Do you remember, this is where we met 10 years ago.
Ich bin glücklich, solange Du an meiner Seite bist - I'm happy as long as you're by my side.
Es liegt an Dir, nicht an mir - It depends on you, not on me.
Maria hat am 5. Mai Geburtstag. Sie wird dieses Jahr 13. - Maria's birthday is on May 5th. She turns 13 this year.
'am' is a short form of 'an + dem'
Note: Special thanks to my neighbour Mr. Rene Sommer for the corrections.