Much of this article is based on information I read in a chapter of "The Tragedy of the Moon" a non-fiction book by Isaac Asimov.
The concept of a week came from the Babylonian Civilisation. Months were based on tracking the lunar cycle (roughly every 29.53 days) and every full moon there was a festival called Sappatu which roughly meant "Full Moon". In the Jewish population within Babylon this word was translated into Sabbath. When the Jewish Exile from Babylon happened around 597 BCE the word diverged from its original meaning and started referring to all four interesting points of the lunar cycle - Full, Half, New, Half again.
This happens every 7.38 days, but this was rounded down to 7 for ease of use and the connection to actual phases of the moon was dropped. This differed to the original Babylonian use where they just occasionally had extra days to keep it synced up. (On a side note they occasionally had 13 months in a year to do the same thing with the solar cycle, which makes me happy. * )
During the early 4th century CE, the first Christian Emperor Constantine the Great made the week part of the Roman system and instituted the names of the days. The names of the seven heavenly bodies were applied to the days of week and through them the Roman pagan gods. The heavenly bodies referred to the objects which moved across the sky (as opposed to the stars which remained fixed) which up until Uranus was discovered in 1781 were all we knew. They were The Sun, The Moon, Mercury, Venus, Mars, Jupiter and Saturn.
If you rank them by the speed at which they go across the sky then you get the list: Saturn, Jupiter, Mars, The Sun, Venus, Mercury and The Moon. If you label each hour of the days using that list you get Day 1, Hour 1 is Saturn. Hour 2 is Jupiter etc. By the time you get to hour 8 you have to start repeating the list with Saturn. Day 1, Hour 24 would be Mars, so Day 2, Hour 1 is the Sun.
It was this first hour of the day which would be associated with the day as a whole so you got each day as: Saturn, The Sun, The Moon, Mars, Mercury, Jupiter, Venus and then back around to the start. In Jewish tradition the Sabbath was on a Saturday and so they put Saturn at the end but kept the rest of the list in the same order.
In Latin the days where know as dies Solis (Sun), dies Lunae (Moon), dies Martis (Mars), dies Mercurii (Mercury), dies Jovis (Jupiter), dies Veneris (Venus) and dies Saturni (Saturn). All Romance languages still have their names of the week based on these Latin roots and if you know your French, Italian or Spanish you will recognise the similarities.
However English and German based their names on the Teutonic pantheon rather than the Roman one. So while Sunday (Sonntag) and Monday (Montag) just take their names directly from the Sun and the Moon, the rest were named after Tiu, Woden, Thor and Freya. Saturn had no direct equivalent in Norse so we retained the Roman pagan name. Most other languages substituted some word meaning Sabbath to get around this problem.
So all in all, the system is a mess which has come through five religions to get to its current naming pattern: Babylonian, Judaism, Roman Pagan, Christianity and Norse.
* Hey a footnote. On the topic of extra months in years; 46 BCE had 14 months and a total of 445 days. The Romans hadn't added in extra months for a long time and did their catching up all at once. It was known as The Year of Confusion (annus confusionis). More on this in my article Odd Dates from History.