Symbolism, Imagery, Allegory
Since Saul misguidedly persecuted and massacred a people called the Gibeonites in his zeal to promote Israel's God, God afflicts Israel with a three-year famine. In order to end it, David needs to ask the Gibeonites how they want to be compensated. The Gibeonites say they're not interested in gold or silver—they're not about greed… just about vengeance on the children of the guy who persecuted them. They want to impale seven of Saul's sons. David says, "Well—different strokes for different folks" (we're paraphrasing).
So the Gibeonites get what they want. It's a classic example of the sons inheriting "the sins of the father"—rather unfairly, as it likely seems to some. But, hey, it ends the famine, since it occurs right before the barley harvest. This is probably related to the notion that a human sacrifice can help revive fertility, which is a belief found in many different cultures.
Technically, this isn't supposed to be a sacrifice—just a method of vengeance. But the ghost of that image, a dying king (or the sons of a king) who is sacrificed to revive the land, lingers on in modern literature. For instance, it's one of the major symbols in T.S. Eliot's masterpiece, "The Waste Land."