In Star Trek, there exists a field of negative energy, known as the Galactic Barrier. Later when the Enterprise-D was thrown into a different galaxy, however, they did not experience the barrier. Such a barrier, to a society capable of warp travel, would seem to be a form of containment. Is it ever explained who (or what) made this barrier, and why?
In strict canon, nobody knows.
Greg Cox's Star Trek: The Next Generation - Q Continuum novels state that the galactic barrier was in fact created by the Q Continuum as a method of preventing the powerful being known only as "0" from returning to the Milky Way Galaxy, from which he was banished for his destruction of the Tkon Empire. These novels also explain that the telepathic powers that occur as a side effect of exposure to the barrier in telepaths are caused by the power of the Q that exist in the barrier, while those who develop a god complex are 'infected' by the presence of 0, who constantly attempts to affect beings on the other side to give himself access to the galaxy once again.