Paul mentions having been imprisoned a number of times, but this answer is only concerned with the occasions on which he was given the liberty to write letters. The Epistles to Philippians, Ephesians, Colossians and Philemon are often called the 'Prison Epistles' because they appear to have been written by Paul while in prison. Of these, only Philippians and Philemon are undisputed.
Most critical scholars date Colossians to the 70s of the first century and Ephesians about ten years later, attributing them to anonymous authors writing in Paul's name. Alvar Ellegard says in Jesus: One Hundred Years Before Christ, page 148, that many passages in Ephesians seem to be directly copied from the ‘presumably somewhat earlier’ Colossians. Thus, the similarities in the two epistles, including references to imprisonment, result from copying and not from Paul's own experience. Burton L Mack says in Who Wrote the New Testament, page 183, that the letters to the Colossians and Ephesians are not authentic, and that there is not a suggestion of the real Paul in either of them. He says the style is different, the vocabulary is different and the rhetoric is different from authentic Pauline letters. If indeed Colossians and Ephesians were pseudepigraphical, we need only look at Paul's liberty to write in the cases of Philippians and Philemon.
When writing the Epistle to Philemon, Paul was undoubtedly a prisoner, but he also tells us that he expected to be freed soon:
Philemon 22 But withal prepare me also a lodging: for I trust that through your prayers I shall be given unto you.
Whatever the circumstances, Paul's imminent freedom in this case may well have meant that he was given considerable liberty pending his release.
In Philippians 1:7, we learn that Paul has been imprisoned, but he then says (1:13-14)that his imprisonment has become so well known that it has served to encourage others to proclaim the gospel:
Philippians 1:13-14: So that my bonds in Christ are manifest in all the palace, and in all other places; And many of the brethren in the Lord, waxing confident by my bonds, are much more bold to speak the word without fear.
The situation is serious for Paul, for it appears he is unsure if he will die, but does not fear death:
Philippians 1:20: According to my earnest expectation and my hope, that in nothing I shall be ashamed, but that with all boldness, as always, so now also Christ shall be magnified in my body, whether it be by life, or by death.
There are several possibilities for the location at which Paul was imprisoned when writing Philippians and, among others, those who rely on Acts of the Apostles consider Rome the probable location, but this is far from certain. He might also have been imprisoned at Caesarea or any of several other locations. Either way, the strength of the charges against Paul might have influenced the amount of liberty he was given. Notice that in Philippians 1:7, Paul says his imprisonment had become so well known that it has served to encourage others to proclaim the gospel, and again in 1:14 they are "much more bold to speak the word without fear." As a cause célèbre, Paul might have been given some liberties, including the opportunity to write this letter, in order to placate his followers.