A variational approach to the study of capillarity phenomena