"If—" jumps directly into giving advice: using apostrophe, the speaker addresses an unidentified "you" with some choice guidance about how best to live life. At the very end of the poem, it'll become clear that the "you" here is the speaker's son, but in these opening lines, it might just as easily be the reader. The advice the speaker has to offer, this structure suggests, applies not just to the son he's speaking to, but to people in general (or rather, as the poem will later make clear, to men in general).
The speaker starts a lot of ideas in these first lines, but he doesn't finish them. Instead, using anaphora to extend his thought, he starts to build a collection of advice without coming to a conclusion about what, exactly, will happen if the reader does what he advises:
If you can keep your head when all about you
Are losing theirs and blaming it on you;
If you can trust yourself when all men doubt you,
This parallel construction repeats through the whole poem: in fact, the poem is one long sentence built from similar clauses. The suspense this shape creates draws readers through the poem, making them wonder what all this advice will lead to.
First and foremost, the speaker emphasizes the importance of keeping one's cool even when everyone else is going out of their minds. The speaker also advises that his reader should remain calm if others "blam[e]" him for something that has gone wrong. Similarly, the son should "trust [him]self" when everyone else "doubt[s]" him, but he should also acknowledge that he's not perfect and that others might have good reason for "doubting" him.
Both of these suggestions show the speaker's belief in the importance of maintaining one's composure, poise, and humility. The general message here is that one should never let emotions get the best of one, and should avoid stooping to the level of one's enemies. This general outlook borrows from Stoicism, an Ancient Greek philosophy that urges people to resist the influence of extreme emotion, regardless of whether that emotion is pleasant or painful.
These lines establish the speaker's use of iambic pentameter, a meter in which each line contains five iambs, or a foot consisting of an unstressed syllable followed by a stressed syllable (da-DUM). Take, for example, line 2:
Are los- | ing theirs | and blam- | ing it | on you;
This bouncy rhythm couches the speaker's stern advice in musical language: there's upbeat energy here, not just admonitions.