The direct answer is simple -- the US Constitution (Article II, Section 1, Clause 2) provides that electors are appointed in the manner that state legislatures direct -- so the answer is the state legislature decides. But state legislatures don't exist in a vacuum, and the reality is not so simple.
State legislatures direct the manner of choosing electors by passing state laws, which, in the case the state decides to choose electors by holding some sort of popular election, include laws for how and when ballots can be cast or received, and how recounts are to work, etc. Normally, state laws are interpreted by state courts, and state courts can strike down or modify state laws if they find that the state laws violate the state constitution. And more fundamentally, state legislatures derive their authority from state constitutions, so in principle, state legislatures should not be able to perform any act that does not conform to the state constitution (which is interpreted by the state courts).
However, the Supreme Court decision in Bush v. Gore (2000) overturned the Florida state supreme court ruling on how to conduct recounts in the Florida election for presidential electors. Normally, federal courts do not get involved in state law issues, but the decision was mostly made on equal protection grounds, and the equal protection clause is part of the federal Constitution. So federal courts can get involved if there is an issue that relates to federal law or Constitution. In the Bush v. Gore case, there was an interesting concurring opinion that said the Florida supreme court had acted contrary to the Florida legislature, but this opinion was only joined by 3 justices, so it is not binding.
Recently, the issue came up again when the US Supreme Court declined to stay a decision by the Pennsylvania state supreme court that said the Pennsylvania constitution required mail ballots postmarked by Election Day but received up to 3 days after Election Day to be counted, even though the state law said they must be received by Election Day. Notably, although the US Supreme Court declined the stay, the order listed that 4 conservative justices dissented and said that they would have granted the stay. This was before Justice Barrett was confirmed, so, now that she is confirmed, depending on her legal position on this issue, there might now be a majority of 5 justices that would intervene against the state court if such an issue were to come up again. The order did not list the legal reasoning for each of the dissenting justices, so it is unclear what legal justification they had in mind in agreeing to overrule the state court.