The Bible reveals not only when and why John the Baptist was put in prison for what would amount to almost two years, it also records the events that led to him being brutally killed by beheading. The Scriptures that record these details are found in Matthew 14:1 - 12, Mark 6:14 - 28 and Luke 3:19 - 20.
In order to fully understand what happened to John the Baptist and why he was killed we need to briefly cover a little background.
Divorce and Remarriage
Herod Antipas, in 4 B.C. after the death of his father Herod the Great, was made ruler (tetrarch) over Galilee and Perea by the Roman Empire. His wife was named Phasaelis. Antipas had a half-brother (same father, different mother) named Herod II (also called Herod Philip I) who was married to a woman named Herodias. Herod II and Herodias produced a daughter named Salome.
Herod Antipas fell in love with Herodias and, due to his infatuation, convinced her that they should divorce their mates and marry each other. The first century Jewish historian Josephus stated the following regarding their divorces and remarriage.
". . . but Herodias, their sister, was married to Herod (Herod II / Herod Philip I) . . . who (together) had a daughter, Salome; after whose birth Herodias took upon her to confound the laws of our country (Israel), and divorced herself from her husband while he was alive . . ." (Antiquities of the Jews, Book 18, Chapter 5, Section 4, see also Section 1).
Condemnation and Arrest
John the Baptist, after the marriage of Antipas and Herodias, began to openly condemned their union on the grounds it was both adulterous (Leviticus 20:21) and incestuous. Leviticus 18:7 - 15 labels as sinful any sex between a person and their relatives (e.g. mother, father, stepmother, stepfather, etc.) which includes intercourse with the wife of a brother or half-brother.
The outrage over John's comments finally reaches a point where Antipas, prodded by Herodias, arrests him and puts him in prison (Mark 6:17, Matthew 14:3).
The Bible states that John was imprisoned between the time of the first Passover of Jesus' ministry (John 2:13) and Pentecost when Christ preached in Nazareth (Luke 4:16 - 21). Luke records the event as follows.
But Herod the tetrarch (Herod Antipas), after being reproved by him (John the Baptist) for marrying Herodias, the wife of Philip (Herod II) his (half) brother, and for all the evils that Herod had done, added this to all that: he locked up John in prison (Luke 3:19 - 20, HBFV throughout).
John is put in prison between April 8 (Passover) and June 1 (Pentecost) in 27 A.D. (Harmony of the Gospels in Modern English, 3rd edition, page 66).
Differing Views
It should be noted that Antipas and his wife entertained different views on what to ultimately do with John. Herodias simply hated him and wanted him killed but lacked the ability or power to do so (Mark 6:19).
Antipas, however, may have initially wanted John the Baptist dead (Matthew 14:5), but opted to just keep him in prison. Part of the reason for his change was that he feared what the people would do if he killed him (Matthew 14:5). Herod also felt like he was protecting him from his wife and, surprisingly, liked listening to him!
. . . Herod feared John, realizing that he was a righteous and holy man, and kept him safe. And after hearing him, he did many things; and he listened to him gladly (Mark 6:20).
John Is Killed
In the early part of 29 A.D., at an unknown time before Passover (John 6:4), Herod hosts a celebration for his birthday with important political and military leaders in attendance (Mark 6:21).
Salome, during the celebrations, dances for Herod. Her dance so pleases him that he impulsively and foolishly promises to give her up to half his kingdom (Mark 6:22 - 23. Matthew 14:6 - 7). Unsure what to ask for, she takes her mother's advice and requests the head of John the Baptist on a platter.
Herod is visibly grieved at Salome's request. He grants what she wants, however, out of concern for how he would be viewed if he reneged on his oath.
Now although the king was personally very sorrowful, because of the oaths and those who were sitting with him, he would not reject her request (Mark 6:26, see also Matthew 14:9).
John the Baptist is beheaded and his head, on a platter, is presented to Salome who then gives it to Herodias. After he is killed his disciples come, bury his body, and then inform Jesus of what transpired (Mark 6:29, Matthew 14:12).