How to derive the binomial test? From what I understand from a pervious question I asked, the p-value for a particular binomial test (specifying a number of flips, and number of heads and tails, and a null-hypothesis to test against) loosely describes the probability that this data is expected with the assumption that the null-hypothesis is correct.
So if I do a binomial test for binom_test(numberOfHeads=9, numberOfFlips=10, weight = .5), and obtain a pvalue for the 2-tailed test. This represents the probability that such an event (nine heads in 10 flips) could occur by a fair coin. If this number is very small, then we can assume that the null hypothesis is rejected with some level of statistical confidence.
My issue is that I'm having a lot of trouble understanding what the derivation of the binomial test. In python there is a binomial test function, but they simply link to the wikipedia article on binomial test.
Intuitively if my null hypothesis is p=1/2, shouldn't my binomial test just be the binomial distribution? For example, I would have thought that my pvalue getting 4 heads in 5 flips would be just the probability of a fair coin getting 4-heads in five flips. So  ${5 \choose 4} (1/2)^4(1/2)^5$. Is this correct? I am looking at what is done on wikipedia and it doesn't seem as though it's in agreement with my thoughts - but what they have written (particularly for the two tailed test) is very unclear to me.


shouldn't my binomial test just be the binomial distribution

Beware speaking so loosely that concepts become muddled.
A test is not just a statistic (e.g. it also needs data, hypotheses and a rejection rule for example) and a statistic is not a distribution (though it has a distribution). Given the assumptions, the test statistic will have the null distribution (a binomial with success probability $\frac12$) when the null is true, and some other distribution (a binomial with a different success probability if the assumptions otherwise hold) when it is false.

This represents the probability that such an event (nine heads in 10 flips) could occur by a fair coin

Again your difficulty appears to be speaking too loosely and confusing yourself - or perhaps instead you have not seen it correctly defined; in any case, it is not correct. Rather the $p$ value here is "the probability of an outcome at least as extreme" as $9$ heads (given $H_0$); the cases you count here are $0, 1, 9$ or $10$ heads, all the cases at least as far from half the cases being heads as $9$ heads is.

would have thought that my pvalue getting 4 heads in 5 flips would be just the probability of a fair coin getting 4-heads in five flips. So ${5\choose 4} (\frac12)^4(\frac12)^5$. Is this correct?

No, for the same reason. With a two tailed test the cases that are at least as extreme as $4$ heads for $5$ tosses are $0, 1, 4$ and $5$ heads.
[Note also that - since the test statistic is discrete - we cannot achieve just any type I error rate we choose, so the use of $p$ values without considering the available significance levels can sometimes be misleading; I often see people compare their $p$ values to $0.05$ without even checking to see whether they can even get below $0.05$. For the $10$-toss case the available two tailed significance levels anywhere near typical significance levels are  about $0.2\%$,  $2.15\%$ and $10.94\%$, so a rejection rule of "reject when p≤0.05" would correspond to $\alpha=2.15\%$. For the 5-tosses case there's no two tailed significance level below $6.25\%$. Choose your rejection rules with care, or you might have a test that can never reject the null.]

loosely describes the probability that this data is expected with the assumption that the null-hypothesis is correct.

This is so loose as to be potentially misleading. It's easy to lead yourself into misinterpreting that phrasing; it's only correctly interpreted when you will take that to mean something equivalent to "the probability of a test statistic at least as extreme as the one from our sample, under $H_0$".
