Wireless sensor networks (WSNs) are widely applied in many fields. It is of concern to evaluate and improve the reliability of a WSN to ensure a highly reliable operation in practice. In this paper, the reliability of WSNs is analyzed by considering random failures, energy consumption, environmental randomness, and interference, due to their nonnegligible effects. For a typical WSN consisting of multiple sensor nodes and a sink node, we suppose that the WSN is functional if the number of the sensor nodes that are interconnected and connected to the sink node is no less than a minimum required number. Two novel reliability indices, the generalized terminal reliability, and the average generalized terminal reliability, are proposed to assess the reliability of WSNs, and their calculation methods are provided based on the algebraic graph theory and Monte Carlo simulations. Moreover, an optimization method with three stages is presented to comprehensively optimize the design of a WSN from the view point of reliability. Finally, a numerical example is given to illustrate the effectiveness of the proposed methods.