I am assuming you are talking about a function like

or in general a function that you can graph on x-y axis

This is easily done with calculus and something called a derivative. You see a function can attain its maximum or minimum where the derivative = 0, or it may never reach its max or min if it continues on forever toward infinity or -infinity

Being as this was posted in pre-calc, you probably aren't familiar with the derivative yet, so let me try to explain this with some physics

Imagine throwing a football as far as you can. How high does it get and when does it get to it's highest point?

Well if we consider velocity, let's say that if the football is rising then the velocity is positive (>0) and if the ball is falling velocity is negative (<0)

The ball must attain it's highest point when velocity=0. Why? If the velocity was positive, then the ball would be rising, even if it was .0000001, the velocity is positive so the ball must rise a little bit after

If the velocity was negative, the ball would be falling, so the height would be decreasing. This means the max already happened. So for the max, velocity must =0

What's happening here with velocity and gravity can also be described in terms of calculus and the derivative. Your question is a great one coming from someone who hasn't taken calculus. But the derivative acts just as velocity acts in our football question. When the derivative is >0, the function must keep increasing in value, so we haven't hit a max, and when it's less than 0, we're moving further away from the max.

Take calculus at some point and this will all be clear