We develop a theoretical framework to investigate the two-body composite structure of a resonance as well as a bound state from its wave function. For this purpose, we introduce both one-body bare states and two-body scattering states, and define the compositeness as a fraction of the contribution of the two-body wave function to the normalization of the total wave function. Writing down explicitly the wave function for a resonance state obtained with a general separable interaction, we formulate the compositeness in terms of the position of the resonance pole, the residue of the scattering amplitude at the pole and the derivative of the Green function of the free two-body scattering system. At the same time, our formulation provides the elementariness expressed with the resonance properties and the two-body effective interaction, and confirms the sum rule showing that the summation of the compositeness and elementariness gives unity. In this formulation the Weinbergs relation for the scattering length and effective range can be derived in the weak binding limit. The extension to the resonance states is performed with the Gamow vector, and a relativistic formulation is also established. As its applications, we study the compositeness of the $Lambda (1405)$ resonance and the light scalar and vector mesons described with refined amplitudes in coupled-channel models with interactions up to the next to leading order in chiral perturbation theory. We find that $Lambda (1405)$ and $f_{0}(980)$ are dominated by the $bar{K} N$ and $K bar{K}$ composite states, respectively, while the vector mesons $rho (770)$ and $K^{ast} (892)$ are elementary. We also briefly discuss the compositeness of $N (1535)$ and $Lambda (1670)$ obtained in a leading-order chiral unitary approach.