Yes and no are the standard basic ways in modern English of responding to another person or rhetorically to oneself, yes expressing such notions as affirmation, assent, and agreement, and no such notions as dissent, refusal, and denial. In earlier English, the situation was more complex. Yes and no were used when the stimulus utterance contained a negative word; and yea and nay were used when no such word was present. In the following examples from the early scenes of Hamlet, both of these usages can be seen (the negative words are highlighted).
Item Location Stimulus Response
yes Ham I.ii.230 Then saw you not his face? O yes, my lord
yea Ham I.v.98 Remember thee? Yea, from the table of my memory / I’ll wipe away all trivial fond records
no Ham I.iv.62 But do not go with it No, by no means
nay Ham I.ii.76 Why seems it so particular with thee? ‘Seems’, madam? Nay, it is

This distinction was already breaking down around 1600: yes and no were beginning to take over all functions, with yea and nay becoming restricted to emphatic usage. (They eventually leaving the standard language altogether, though they are still widely used in regional dialects.)
Hamlet displays the new system alongside the old (though there are few instances of yes, as the usual affirmative response in that play is ay). An example of a ‘self-response’ is given as a comparison ( Ham I.v.135), along with an example from a different play. In each case, there is no negative word in the stimulus utterance.
Item Location Stimulus Response
yes Ham I.v.135 I am sorry they offend you, heartily. Yes, faith, heartily.
yes MM II.iii.25 Love you the man that wronged you? Yes, as I love the woman that wronged him
ay Ham I.iv.13 Is it a custom? Ay, marry, is’t
no Ham I.v.119 Good my lord, tell it No, you will reveal it
no Ham II.i.108 have you given him any hard words of late? No, my good lord

Both new and old negative usages can be seen in quick succession in Ham II.ii.334,ff:

Hamlet: Are they so followed?
Rosencrantz: No, indeed are they not.
Hamlet: How comes it? Do they grow rusty?
Rosencrantz: Nay, their endeavour keeps in the wonted pace.

All four words had other functions than direct response. Nay, for example, could also used as an introductory word, expressive of doubt or reservation, without any direct response being made to the preceding utterance.
Item Location Stimulus Response
nay Ham I.i.2 Who’s there? Nay, answer me
nay Ham II.ii.290 [aside] Nay then, I have an eye of you

wide range of other words and phrases (e.g. content, ha, buzz buzz) could of course also be used as responses: DISCOURSE MARKERS; POLITENESS; EXCLAMATIONS