The origin of the names of most of the days of the week are linked to Roman mythology. The Romans saw a connection between their gods and the changing face of the night-time sky, so it became natural to use their gods' names for the planets. The ones they were able to see in the sky were Mercury, Venus, Mars, Jupiter and Saturn. Those five planets plus the moon and sun made seven major astronomical bodies, so when the seven-day week was imported from Mesopotamia early in the fourth century it was a natural to use those astronomical names for the days of the week.
The first day of the week was named after the sun, followed by the moon, Mars, Mercury, Jupiter, Venus and Saturn. The names of the week were adopted with little change throughout most of the Roman Empire and even beyond. In only a few cases (including Spain) were changes made.
In Spanish, the five weekdays all retained their planetary names. Those are the five days whose names end in -es, a shortening of the Latin word for "day," dies. Thus lunes comes from the word for moon (luna), martes comes from Mars (Marte), miércoles comes from Mercury (Mercurio), and viernes comes from Venus (Venus).
The connection with Jupiter (Júpiter) is not quite so apparent with jueves, Thursday, but it comes from the Latin Jovis Dies, "Jupiter's Day."
In Spanish, that leaves the words for Saturday and Sunday that weren't adopted using the Roman naming pattern. Domingo, the word for Sunday, comes from the Latin word dominica (which derives from "dominus" meaning "lord")." And sábado, the word for Saturday, comes from the Hebrew word Sabbath, meaning a day of rest.