Ich schreibe an die Tafel.
Ich schreibe an der Tafel.
Are they both correct? And what’s the difference?
They are both correct.
The difference is:
Ich schreibe an die Tafel means I write something onto the blackboard (or whiteboard, depending on the setup).
Ich schreibe etwas an der Tafel means I write something while standing at the blackboard. It is not necessary to use the blackboard in that sentence, I could also write something to a sheet of paper while standing there.
The first stresses the action of writing to a blackboard, the second stresses my position when writing something (that still MIGHT be writing on the blackboard, but not necessarily).
German has nine dual-way prepositions, these are an, auf, hinter, neben, in, über, unter, vor and zwischen. For those nine prepositions (and only for those), the preposition+accusative means a different thing than preposition+dative. With verbs of motion, this is usually the difference between direction and position.
Ich gehe an den Strand. (accusative)
I walk to the beach.
Ich gehe am/an dem Strand. (dative)
I walk on the beach.
The exact meanings depend on the verb. For schreiben, it's
Ich schreibe (etwas) an die Tafel. (accusative)
I write (something) onto the blackboard.
Ich schreibe an der Tafel. (dative)
I write at the blackboard.