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.
 A: 
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$".
A: Thanks to @BruceET and @Glen_b some confusion was clarified, which I figured I'd write here to be helpful for anyone who also had my issue.
The main issue that I had was that I previously did a simulation to look at the frequency at which fair coins produce p-values that would be false-positives.
If I have a p-value of 3.4% and test it for p-value<3.5% (some interval that compensates for the discrete nature), this 3.4% doesn't mean that there is a 3.4% chance of reproducing the same data, but instead is that there is a 3.4% chance that a fair coin with that data would produce a p-value of 3.4%.
What was hard for me to understand, after getting that idea, is that this p-value seems like just an arbitrary function, and we could've used any function and characterized the chance said data would produce a certain output for that function.
But, from what I'm gathering, the utility of the p-value is that it typically adds a weight to all unlikely events. So you're sort of heuristically collecting all probabilities that are as and more unlikely in the parameter of interest and adding them up. Not quite at 100% understanding (and please correct me in the comments), but I think I am at least more correct than I was.
A: There are already some very good answers here, but I figured I would offer my own take.
You're correct in identifying the interpretation of the p value of the test:  The probability of seeing 9 or more heads from a fair coin assuming all other assumptions are correct.
Note that the binomial test is in terms of the coin's bias, not in terms of the number of heads as you might expect for the binomial distribution.  But, I can always obtain the number of heads by multiplying by the number of flips made since $\widehat{p} = x/n$, where $p$ is the estimated bias, $x$ is the observed number of heads, and $n$ is the number of flips.
Because $n\widehat{p}$ is an estimate of the expectation of the number of heads, it has some desirable properties.  Due to the central limit theorem, we know
$$ n\widehat{p} \sim \operatorname{Normal}\big(np_0, np(1-p_0)\big) $$
where $p_0$ is the true bias. This implies
$$ \dfrac{\widehat{p} - p_0}{\sqrt{\dfrac{\widehat{p}(1-\widehat{p})}{n}}}\sim \operatorname{Normal}\big(0, 1\big) $$
Here, I've replaced $p_0$ with $\widehat{p}$ in the expression for the variance so align with the common Wald test statistic.  Keeping $p_0$ in the variance expression would result in a Score test.
This is the test statistic for the binomial test.  Our test statistic is based off a normal approximation to the binomial distribution, which becomes better when $n \to \infty$.
