The calculation
Journals within a subject category are ranked by a metric, then split into four groups of roughly equal size. Clarivate publishes the formula for Journal Citation Reports: with X as the journal’s rank and Y as the number of journals in the category, Z = X/Y gives Q1 at 0 < Z ≤ 0.25, Q2 at 0.25 < Z ≤ 0.50, Q3 at 0.50 < Z ≤ 0.75, and Q4 above that.
The groups are not exactly equal, because of ties. Clarivate uses sparse ranking: journals with the same impact factor share a rank and therefore share a quartile. Ties became materially more common when the impact factor moved from three decimal places to one, which is worth knowing if your journal sits near a boundary.
Why "of its category" is the whole point
Citation behaviour varies enormously between fields. A journal that would be mid-table in cell biology can be the top-ranked title in a mathematics category with a fraction of the impact factor. Neither journal is better; they are being measured against different populations.
This is also why comparing quartiles across fields tells you very little, and why funders and promotion committees that treat "Q1" as a single universal tier are applying a blunter instrument than the metric supports.
Where quartiles do not exist at all
Not every indexed journal has a quartile. Clarivate assigns no rank, quartile or percentile to the 25 arts and humanities categories, on the grounds that ties and category sizes there would produce skewed distributions. So a journal can carry a Journal Impact Factor and have no quartile whatsoever.
That matters for humanities researchers under institutional pressure to publish "in a Q1 journal" — in some categories the target does not exist, and the requirement needs renegotiating rather than chasing.
The 25 Web of Science arts and humanities categories receive no quartile at all — a journal there can have an impact factor and no Q.