Short version: the assignment was more astrological than religious, insofar as such things can be separated out.
The oldest explanation I can find of the now-standard day names is from Cassius Dio, second century CE, though the names are referenced earlier (Tibullus, first century BCE). Dio claims the system was developed by the Egyptians, but most likely the Egyptians adopted it from the Babylonians.
According to Dio, the cycle "Saturday, Sunday, Monday" etc comes from astrology. In other words, the days aren't associated with the gods directly, but with the planets, which are then astrologically connected to the gods.
In the classical geocentric model, there were seven planets: Saturn, Jupiter, Mars, Sol, Venus, Mercury, Luna. Saturn was the farthest away, while Luna was the closest. As Dio explains it, each hour was governed by one of these planets. (This is one reason historians think the model started with the Babylonians—they were the ones who divided the day into twenty-four hours.)
τὰς ὥρας τῆς ἡμέρας καὶ τῆς νυκτὸς ἀπὸ τῆς πρώτης ἀρξάμενος ἀριθμεῖν, καὶ ἐκείνην μὲν τῷ Κρόνῳ διδούς, τὴν δὲ ἔπειτα τῷ Διί, καὶ τρίτην Ἄρει, τετάρτην ἡλίῳ, πέμπτην Ἀφροδίτῃ, ἕκτην Ἑρμῇ, καὶ ἑβδόμην σελήνῃ,
If you start enumerating the hours of the day and night, starting from the first one, and assign the first one to Cronus, the second to Zeus, the third to Ares, the fourth to Helios, the fifth to Aphroditë, the sixth to Hermes, and the seventh to Selenë,
κατὰ τὴν τάξιν τῶν κύκλων καθ᾿ ἣν οἱ Αἰγύπτιοι αὐτὴν νομίζουσι, καὶ τοῦτο καὶ αὖθις ποιήσας, πάσας τε οὕτω τὰς τέσσαρας καὶ εἴκοσιν ὥρας περιελθών, εὑρήσεις τὴν πρώτην τῆς ἐπιούσης ἡμέρας ὥραν ἐς τὸν ἥλιον ἀφικνουμένην.
according to the order of the cycles that were developed by the Egyptians, and if you do all this again, going through the full twenty-four hours, you'll discover that the first hour of the second day is assigned to Helios.
καὶ τοῦτο καὶ ἐπ᾿ ἐκείνων τῶν τεσσάρων καὶ εἴκοσιν ὡρῶν κατὰ τὸν αὐτὸν τοῖς πρόσθε λόγον πράξας, τῇ σελήνῃ τὴν πρώτην τῆς τρίτης ἡμέρας ὥραν ἀναθήσεις, κἂν οὕτω καὶ διὰ τῶν λοιπῶν πορεύῃ, τὸν προσήκοντα ἑαυτῇ θεὸν ἑκάστη ἡμέρα λήψεται.
And if you do all that again in just the same way for the next twenty-four hours, then the first hour of the third day will be given to Selenë, and if you keep going through the remaining days, each and every day will be assigned its respective god.
ταῦτα μὲν οὕτω παραδέδοται.
So, that is the traditional way of it.
(Dio XXXVII.19, translation mine)
That is, each hour is assigned to one of the planets, in order; since 24 ≅ 3 mod 7, each day starts three places further around the cycle. So if you assign each day to a planet, based on its first hour, you get Saturn, Sol, Luna, Mars, Mercury, Jupiter, Venus.
When different cultures adopted this system, they fit their own gods into it as best they could, but used the same assignment to planets. According to the astrologers, for instance, the planet Jupiter was associated with strength and storms (rather than e.g. kingship), so the Germanic peoples connected it with their god Thor. "Tuesday" was the day of Tyr (Germanic), Mars (Roman), Ares (Greek), or Nergal (Babylonian), "Friday" was the day of Freyja (Germanic), Venus (Roman), Aphroditë (Greek), or Ištar (Babylonian), and so on.