Where does the use of "why" as an interjection come from?
According to the OED, the use of why as an interjection dates back to the 16th century:
1519 Interl. Four Elem. Bvij, Than I perceyue ye wyll make gode chere. Hu. Why, what shulde I els do?
1581 Confer. with Campion (1583) Ciij, Why, is not Saint Iames Epistle called the Catholike Epistle of Saint Iames. How do you then denie it to be Canonicall?
1599 Shakes. Much Ado iv. ii. 44 Why this is flat periurie, to call a Princes brother villaine. Ibid. v. iv. 73 Bene. Doo not you loue me? Beat. Why no, no more then reason.
The OED doesn't explain why it is used in that manner. I can only speculate. First it was just a question expressing doubt reduced to its essence:
It is best that we negotiate a truce with Spain. — Why is that so? I believe the Spanish armies cannot threaten us.
They will fail in their attempt to conquer England. — Why? Their fleet is far superior in strength and number.
This developed through rhetorical question into a mere interjection of surprise:
We should offer the English crown to the Dutch Stadtholder. — Why, William of Orange, you say? That would be a terrible idea.
From there its function of calling attention (if that's what it is) to a following statement developed:
This Corsican might be trouble; why, I think he will conquer all of Europe in a decade.
The OED notes that it is often used this way in opposition to a vague doubt or apprehension.
"why" can be compared to an old Latin form qui, an ablative form, meaning how. Today "why" is used as a question word to ask the reason or purpose of something. This use might be explained from a formula such as "How does it come that ...".
If you meet an old friend of yours, whom you never expected to meet in town, you can express your surprise by saying: Why, it's Jim!
This why in the meaning of "how", in Latin qui, can be explained as rest of a whole sentence: How is it possible? It's Jim, whom I never expected to see again!
This is my personal view of the matter. I don't suppose that everybody will agree.