HAVING: Filters AFTER grouping, on aggregate results. WHERE filters rows before grouping; HAVING filters groups. HAVING filters groups after GROUP BY has done its work. WHERE filters individual rows before grouping; HAVING filters on the results of aggregates. SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id HAVING COUNT(*) > 5 finds customers with more than five orders. You cannot write that condition in WHERE, because the count does not exist yet at that point.
Use both together when you can. Filter as many rows as possible with WHERE first, such as orders from this year, then filter groups with HAVING. That way the database groups fewer rows and the query runs faster. A condition on a plain column belongs in WHERE, even though HAVING would technically accept it in some databases.
What does an example of HAVING look like?
SELECT user_id, COUNT(*) AS n FROM orders GROUP BY user_id HAVING COUNT(*) > 5;