Thereby, as a dictionary would tell you, means "by that means" and similar.
Poetry makes it hard to parse things. Partly because the desire to fit rhyme and metre means that odd sentence structures and word choices are used, and partly because the line breaks may or may not have grammatical significance.
In this case, I would propose to ignore the line breaks when considering the grammar/syntax, and just look at the punctuation.
And therefore, friend, if your great race were run and these things came, so much the more thereby have you made greatness your companion
For the sake of understanding this, let's lose the "and therefore, friend," because it has no impact on the meaning of the rest. The rest of the sentence that follows on subsequent lines are similarly irrelevant to the basic meaning of the lines in question. We now have a simple conditional:
"If your great race were run and these things came, so much the more thereby have you made greatness your companion."
The first part of this, the conditional itself, is just setting up the rest of the sentence, a condition upon which the rest depends; if "your great race were run" (whatever that means; it's poetry, so it's fairly common for it not to be obvious) and "these things came" (meaning 'these things happened', and again we don't know what 'these things' are in any trivial way), then...
Well, if those conditions are fulfilled, the rest of the sentence would be expected to come true - and this is the bit you are struggling with.
"so much the more thereby have you made greatness your companion"
Thereby refers back to the great race being run and 'these things' coming. By the great race being run and those things coming, 'you' have made greatness your companion (i.e. achieved greatness, in poetic language) so much more.