The siman on ‘tzida’ (trapping) is rather long and complex. We will stick to our abridged approach and hopefully cover the most commonly relevant halachos.
MBY 316:1-12 Forbidden and Permissible Trapping (abridged)
[Based heavily upon Kitzur Shulchan Aruch: 80:52-53 and 87:20-21, “Metsudah” edition, with notes, translated and annotated by Rabbi Avrohom Davis]
80:52) It is forbidden even to catch a flea on Shabbos. But if it is on your body and it is biting you, since it causes you physical discomfort, it may be removed and thrown away. It is forbidden to kill it, because it is forbidden to kill any living creature on Shabbos. (Footnote #162: In a place where many ants are found, one must be careful not to step on them - Shulchan Aruch Harav 316:21.) Lice, however, since that are created only by perspiration, are permitted to be killed. Nevertheless, those found in clothing must not be killed; rather, they should be removed and thrown away. (Footnote #163: Because fleas are found there too and you may come to kill a flea - Magen Avraham) Only lice found in the head may be killed.
80:53) You must take care when closing a box or vessel in which there are flies, to let them fly out first, because when you close it they will be trapped there. However, it is not necessary to examine the box to see that none are left; it is sufficient to chase away those you see.
Here is a bit of supplement from Rabbi Ribiat in The 39 Melachos, vol. III, p. 875: Non-stinging insects. Only the threat of significant physical pain or injury can permit trapping. Insects that are merely annoying (e.g. house flies, ants, roaches, etc.) or whose stings cause only mild pain and discomfort (in most cases, even mosquitos) may not be trapped. Trapping is prohibited even if buzzing and interference of the insect disturbs one at his meal or is disruptive to his solitude.
87:20) Animals, beasts and fowl that are not yet trained to return to their cages in the evening – or, even if they were trained to do so, at the moment, they have “rebelled” and are not under the control of their owners - it is forbidden to drive them back into their cages or into the house. Even if they are in the house or inside the cage, but the door is open, it is forbidden to close it, because, as a result they are being caught, which is a violation of the prohibition of ‘tzida’ (trapping). (Footnote #42: The Torah melacha of ‘tzida’ applies only to those animals and birds that people usually capture or trap for their use. It also applies only where the capture puts the creature into a situation where you can now get hold of it with a single swoop; for example, within the confines of a small room. However, if the room is large and you cannot easily get hold of the animal, then closing the door of such a room when there is an animal inside, would only be prohibited mid’rabanan – Biur Halacha 316:6.)
87: 21) If the creatures are already trained and domesticated and usually return home in the evening, but they are out now, and you are worried that they will be stolen, you are permitted to drive them to a safe place, but you may not carry them by hand because they are ‘muktzah’.
More from Rabbi Ribiat Ibid., p. 878-9: There is no firm basis for permitting one to close the door when entering or leaving the house, if a non-domesticated animal notices the opening and attempts to flee. Closing the door in that case would be a forbidden act of ‘tzida’ (at least mid’rabanan.) However, if the house is very large or has many rooms, one may confine the animal to a section of the house (i.e. in an area large enough for the animal to elude capture.) Reducing and closing off part of the area of an animal’s loose confinement is not forbidden if it still remains loosely confined. A long-term solution is to design the entrance of the house with a small foyer containing two doors. Entering would then be permitted if one is careful to close the outer door before opening the inner door. (Ed: There are other methods which are permissible. See more in the aforementioned source.)