Generally speaking: Write in English. Write on a paper (or on a hand).
The song, however, chose 'in' to evoke "being part of" and play with the idea of "permanence."
Are we written in the stars? = are we written among the stars/as part of the stars
*You can think of "in" here as "included" or "inside" or "among"
*Write on a star = writing on the surface of a star, which isn't the image the song wants to evoke
Or are we written in the sand? = amidst the sand
*When you write in the sand, you have to etched or dig a part of it, so it's IN the sand. It's not on the surface of the sand, it's within it. See Elsa's comment above.
*Write on the sand = write on the surface. Maybe you used water or paint or sticks or something you can put on the surface of the sand, which again isn't what the song is trying to make you imagine
Are we written in the stars, baby, or are we written in the sand?' = Is this permanent or just a passing thing?