Energy-preserving methods for differential equations