Simple past tense is used when we have to convey about an action that happened in the past and is over in the past.
For example,
I worked in XYZ two years back.
I don't work there anymore and there is no continued effect or so in the present. Its over i.e. I no more work there.
Past perfect tense: It is used when we have to show sequencing between RELATED EVENTS (the word related holds importance here).
You gave an example: Example 1
Right: By 1945 , the United States had been at war for several years .
US had been at war.......................By 1945 (shows that the event was over by this year and no more continues. Therefore past perfect is used for the first event).
'By 1945' acts as an event here. This phrase 'by 1945' shows that something (some event) happened before.
If we had to show that US is still at war then this would be the construction:
Since 1945, the United States has been at war.
Since 1945...................till today (still continuing)
Whenever there are two related events in a sentence, we need to use past perfect for the earlier event to show that it happened first. Otherwise there will be a confusion as to what happened first. For example,
Joe was very disappointed when he saw his scores in the GMAT because he had hoped for a 770.
Now see.....Joe hoped for 770 before he saw his score in the test. Therefore we use past perfect for the first event and simple past for the second one.
Example 2
Wrong : Joe learned about an Epoch in which dinosaurs had walked the earth.
Joe learned something.....What? He learned that dinosaurs walked the earth. Is the action of Joe's learning in anyway related to the second action ?
No. The first event here in no way affects the second event of joe's learning about dinosaurs.
If we use simple past for both the events here:
Joe learned about an Epoch in which dinosaurs walked the earth.
Its pretty clear that dinosaurs walked a long time back (its a fact. Right?) and there is no way in which these two events could happen together.
Some sentences clear the sequencing of events by presenting some fact like in the above, by using words such as after and before.
I hope this helps. If you find some error in the above, please let me know.