First, I'm not really qualified to answer this one, but it's gone unanswered for a few days so I thought I'd give it a shot. If I'm wrong about anything then I assume someone will comment about it and we'll both learn something.
I think if a subclause is truly the subject or object of the main clause then the rule is fairly simple: if the subclause is included after where you'd use a noun, then you put es as a placeholder in the main clause:
Es ist wichtig, genug Schlaf zu bekommen.
(It's important to get enough sleep.)
Sie sagten es uns jeden Tag, genug Schlaf zu bekommen.
(They told us every day to get enough sleep.)
If the subclause is used in the same position a noun would normally go, then the es isn't needed:
Genug Schlaf zu bekommen ist wichtig.
(To get enough sleep is important.)
Jeden Tag sagten sie uns, genug Schlaf zu bekommen.
(Every day they told us to get enough sleep.)
But I think this is normally done with a zu clause, which isn't covered in any of your examples. So I think what you're really talking about is a type of relative clause. In this type of the clause the entire clause isn't the subject or object, but a certain element within it which is marked with a relative pronoun, either der (properly inflected), certain question words such as was, da- or wo-. A general summary of relative clauses is given at Dartmouth and at UW-M#1 & UW-M#2.
Both Dartmouth and UW-M give rules for when to use d relative pronouns vs. w relative pronouns. The upshot seems to be that you use d when the noun being referred to is "clearly defined" in some sense. For example:
Ich bringe das Buch zurück, das ich gestern geliehen habe.
(I'm returning the book that I borrowed yesterday.)
In this case das Buch is clearly defined so das is used. But etwas doesn't really define anything so (at least according to Dartmouth) it's better to use was with it:
Ich bringe etwas zurück, was ich gestern geliehen habe.
(I'm returning something that I borrowed yesterday.)
The phrase "clearly defined" is actually rather vague, so I gather there is often room for interpretation on whether to use a d word or a w word. But if there is no definition at all then it's certain that the w word would be used.
Ich gebe zurück, was ich gestern geliehen habe.
(I'm returning what I borrowed yesterday.)
Wir fahren, wohin noch niemand zuvor gefahren ist.
(We're going where no one has gone before.)
In the d word case, it seems unlikely that es would be needed because the noun would have already been introduced in the main clause. In w case I think it's basically the same rule as for zu clauses, namely use the es as a placeholder if you need to refer to something that is specified later in the sentence:
Es wird zurückgegeben, was ich gestern geliehen habe.
(It's being returned, that which I borrowed yesterday.)
I don't think adding es where it's not needed is really wrong; it may just be redundant or it may change the emphasis a bit.
I think the main point is that all the different subcases you're asking about don't really affect the final outcome. You usually don't have to memorize every possible permutation of a rule once you know the general principle. There are some rules which have oddball exceptions; the rule about no articles when you're describing someone's profession (e.g. Ich bin Student) comes to mind. But you usually don't discover that kind of thing by asking about every possible such exception, as in "What about vehicles? What about buildings? What about animals? etc." until you finally hit on "What about professions?" A good grammar or a good teacher will explain this kind of thing when it occurs without your having to ask so many questions.
So I think both versions of the examples you gave are probably correct, though there may be a subtle shift in meaning as Tilman Schmidt mentioned in a comment. As in English, adding an extra indefinite pronoun is rarely wrong and I would think it counts as just a another way to phrase the same basic meaning. The sentences
I'm returning it, the book I borrowed yesterday.
I'm returning the book I borrowed yesterday.
are both correct and both mean about the same thing. And I think the same thing happens in German; whether to include the es is up to your choice.