Direct quote from the above link:
"The designation of Small and Medium divisions must follow the team sizes below:
Small = 5–22 members
Medium = 23–30 members"
So, when the divisions are split, you must be placed based on size. There are times when small and medium are not split and therefore small and medium teams may compete against each other.
This is listed for levels 1-4. The same thing is said for 5/5R but with small/large. And then the same thing again for xsmall/small splits. So no matter when it is split, you will be placed based on the number on the team.
For Worlds divisions, the sizes are laid out in the actual grid, so that would make it the same for those as well.
ETA: The only exception I've seen is when a team wins a bid in a certain division then loses athletes, making them a smaller size. They must still compete in the division they won the bid in (at Worlds/Summit only), regardless of the number on the team at that specific comp. There was a team a couple years ago in large senior that lost athletes and had less than 30 at Worlds, but was still in large because they did not win a new bid as medium. Then of course the new situation with xsmall/small Worlds teams.