It had suspended him while it heard the case but it had been widely expected to reject the opposition parties' petition.
The opposition argued General Prayuth had exceeded his constitutionally-limited eight years in office.
The former military chief seized power in a 2014 coup.
He made himself PM in August that year, meaning he has been the PM now for longer than that.
But the court ruled that his term of office began in April 2017 when the constitution came into effect, and not when he was appointed in 2014.