An elegy is a sad poem, usually written to praise and express sorrow for someone who is dead. Although a speech at a funeral is a eulogy, you might later compose an elegy to someone you have loved and lost to the grave.
The purpose of this kind of poem is to express feelings rather than tell a story. Thomas Gray's “Elegy Written in a Country Churchyard” is a poem that reflects on the lives of common people buried in a church cemetery, and on the nature of human mortality. The noun elegy was borrowed in the 16th century from Middle French élégie, from Latin elegīa, from Greek elegeia, from elegos "mournful poem or song."