Unitary transformations are routinely modeled and implemented in the field of quantum optics. In contrast, nonunitary transformations, which can involve loss and gain, require a different approach. In this work, we present a universal method to deal with nonunitary networks. An input to the method is an arbitrary linear transformation matrix of optical modes that does not need to adhere to bosonic commutation relations. The method constructs a transformation that includes the network of interest and accounts for full quantum optical effects related to loss and gain. Furthermore, through a decomposition in terms of simple building blocks, it provides a step-by-step implementation recipe, in a manner similar to the decomposition by Reck et al. [Experimental Realization of Any Discrete Unitary Operator, Phys. Rev. Lett. 73, 58 (1994)] but applicable to nonunitary transformations. Applications of the method include the implementation of positive-operator-valued measures and the design of probabilistic optical quantum information protocols.