There is always some risk in being merciful
The question quotes the discussion between Théoden and the others about what to do with Wormtongue. It is clear from that discussion that they are aware that Wormtongue may cause problems if he is allowed to go free. Théoden lets him go as an act of mercy in recognition of his earlier service, giving him one last chance to reform.
Imprisoning Wormtongue
The last part of the question asks why Wormtongue wasn't simply taken to Helm's Deep as a prisoner. This does at least seem a logical choice, and is not really addressed in the discussion. I suspect that Gandalf would point to the power of Wormtongue's tongue (he may have learned a little from Saruman). We know that his words were enough to poison Théoden's mind so that he was enfeebled. In The Houses of Healing, Gandalf tells Éomer that Wormtongue was able to infect Éowen (so that she was ready to throw away her life).
‘Think you that Wormtongue had poison only for Théoden’s ears? Dotard! What is the house of Eorl but a thatched barn where brigands drink in the reek, and their brats roll on the floor among their dogs? Have you not heard those words before? Saruman spoke them, the teacher of Wormtongue. Though I do not doubt that Wormtongue at home wrapped their meaning in terms more cunning. My lord, if your sister’s love for you, and her will still bent to her duty, had not restrained her lips, you might have heard even such things as these escape them. But who knows what she spoke to the darkness, alone, in the bitter watches of the night, when all her life seemed shrinking, and the walls of her bower closing in about her, a hutch to trammel some wild thing in?’
The Lord of the Rings Book Five, Chapter 8: The Houses of Healing
Page 867 (Houghton Mifflin Harcourt; Single Volume 50th Anniversary Edition)
This would be part of what Gandalf has in mind when he says of Wormtongue "With safety you cannot take it with you, nor can you leave it behind." So imprisoning Wormtongue would also be dangerous.
Let's look at the consequences of the decision to free him:
The culvert at Helm's Deep
Wormtongue's role in blowing up the culvert in the movie is just an unnecessary detail made up by Jackson (it isn't mentioned in the books). Even if this information came from Wormtongue, there is no reason to assume he told Saruman about it after being released (as mentioned in Edlothiad's answer). I don't consider this a consequence of the decision to free Wormtongue
The Palantír
During the parley at Orthanc, it is Wormtongue who throws the Palantír at Saruman or Gandalf.
At that moment a heavy shining thing came hurtling down from above. It glanced off the iron rail, even as Saruman left it, and passing close to Gandalf’s head, it smote the stair on which he stood. The rail rang and snapped. The stair cracked and splintered in glittering sparks. But the ball was unharmed: it rolled on down the steps, a globe of crystal, dark, but glowing with a heart of fire. As it bounded away towards a pool Pippin ran after it and picked it up.
‘The murderous rogue!’ cried Éomer. But Gandalf was unmoved. ‘No, that was not thrown by Saruman,’ he said; ‘nor even at his bidding, I think. It came from a window far above. A parting shot from Master Wormtongue, I fancy, but ill aimed.’
‘The aim was poor, maybe, because he could not make up his mind which he hated more, you or Saruman,’ said Aragorn.
The Lord of the Rings Book Three, Chapter 10: The Voice of Saruman
Page 583-4 (Houghton Mifflin Harcourt; Single Volume 50th Anniversary Edition)
Using the Palantír, Aragorn is able to learn the state of Sauron's preparation for war and to distract Sauron. This leads him to try the desperate journey through the Paths of the Dead. When he tells Gimli and Legolas about revealing himself to Sauron, we read:
‘The hasty stroke goes oft astray,’ said Aragorn. ‘We must press our Enemy, and no longer wait upon him for the move. See my friends, when I had mastered the Stone, I learned many things. A grave peril I saw coming unlooked-for upon Gondor from the South that will draw off great strength from the defence of Minas Tirith. If it is not countered swiftly, I deem that the City will be lost ere ten days be gone.’
‘Then lost it must be,’ said Gimli. ‘For what help is there to send thither, and how could it come there in time?’
‘I have no help to send, therefore I must go myself,’ said Aragorn. ‘But there is only one way through the mountains that will bring me to the coastlands before all is lost. That is the Paths of the Dead.’
The Lord of the Rings Book Five, Chapter 2: The Passing of the Grey Company
Page 780-1 (Houghton Mifflin Harcourt; Single Volume 50th Anniversary Edition)
Arguably the result here is that Minas Tirith is saved. I consider this a good consequence of the decision to free Wormtongue.
Destruction in the Shire
Wormtongue certainly played a part in the destruction in the Shire. The one thing we know that Wormtongue did was to kill Lotho.
‘Then I will,’ said Saruman. ‘Worm killed your Chief, poor little fellow, your nice little Boss. Didn’t you, Worm? Stabbed him in his sleep, I believe. Buried him, I hope; though Worm has been very hungry lately. No, Worm is not really nice. You had better leave him to me.’
The Lord of the Rings Book Six, Chapter 8: The Scouring of the Shire
Page 1020 (Houghton Mifflin Harcourt; Single Volume 50th Anniversary Edition)
Saruman was the architect of the destruction and would presumably have caused as much havoc if Wormtongue had not been there. We can assume that Lotho or some other hobbit would have died. I don't consider this a consequence of the decision to free Wormtongue
The death of Saruman
Leaving aside the metaphysical question of whether Saruman actually "died", it was certainly Wormtongue who removed him from Middle-earth. Frodo (who has presumably gained a lot of wisdom in these matters), did not want this to happen. When the hobbits want to kill Saruman after his attempt on Frodo's life, Frodo says:
‘No, Sam!’ said Frodo. ‘Do not kill him even now. For he has not hurt me. And in any case I do not wish him to be slain in this evil mood. He was great once, of a noble kind that we should not dare to raise our hands against. He is fallen, and his cure is beyond us; but I would still spare him, in the hope that he may find it.’
The Lord of the Rings Book Six, Chapter 8: The Scouring of the Shire
Page 1020 (Houghton Mifflin Harcourt; Single Volume 50th Anniversary Edition)
Note that he is using a similar argument to the one Gandalf used about Wormtongue. I consider this neither a good nor bad consequence of the decision to free Wormtongue
Conclusion
I didn't judge any of those consequences to be bad, and one of them was actually good. So while Gandalf and Théoden understood that there might have been bad consequences, they freed Wormtongue as an act of mercy and were "rewarded" with a good outcome (as often happens in Tolkien's works when someone makes a moral choice).