Although the phrase is common enough today to be considered a cliché, appearing in popular songs, television shows, and literature, "writing on the wall" is a reference to a specific text written over two millennia ago.
Daniel, a book in the Hebrew Bible (also referred to as the Old Testament by Christians), contains a cautionary tale about a king named Belshazzar who hosts a party for his fellow royalty. During this party, Belshazzar requests that vessels belonging to the temple of God be brought to the party to drink from.
After this is done, a mysterious hand appears and begins to write on the wall. The origins of the hand are not detailed or explored by any of the characters, and the text does not specify whether the hand appears to be attached to an arm hidden by darkness or if it is visibly only as an individual, floating hand.
Belshazzar is upset by the writing on the wall and asks his guests to explain it to him, but no one at the party can recognize the words. The queen suggests that Daniel, who has already been established as a wise character by this point in the book due to his ability to help Belshazzar's father understand the meaning of his bad dreams, may be able to explain the writing, and the king calls on him for help. Daniel cautions the king that, though his father was a good servant of God, he has displeased God and has committed idolatry.
In the King James Version of the Bible, Daniel translates the writing on the wall for the king as "MENE, MENE, TEKEL, UPHARSIN." The words come from Aramaic origins and are explained to the king by Daniel near the end of the chapter as follows: MENE means "God hath numbered thy kingdom, and finished it," TEKEL means "Thou art weighed in the balances, and art found wanting," and UPHARSIN means "Thy kingdom is divided, and given to the Medes and Persians." All of these words, in their original Aramaic meanings, are related to money counting.
Later that night, the king is slain, though the details of his slaying are untold in the book. Because the words appear on the wall immediately after the king's sacrilegious actions and because his death--which is not a natural death, as indicated by the word "slain"--follows almost immediately after the translation, the writing became a symbol of inevitable repercussions.
Though the actual words written on the wall are seldom referenced, the image of writing on the wall lives on today as a warning of negative consequences or simply as indication that a thing will soon be reaching its end. For example, one may say, "The writing is on the wall for Super-Mart," meaning that Super-Mart will be bankrupt or will shut down operations soon. One may also utilize the phrase to reference a specific cause and effect: "That squealing sound the car makes is writing on the wall for the brake pads." The phrase is not used to simply indicate bad luck, but instead relies on a connotation of consequences brought about by previous actions.