I made a map example of this for another guy asking the same question, but can't find it at the moment.
Anyhow, you need triggers and some object editing.
There's two ways of doing this.
#1 The shitty way:
Use Player - Limit training of <Insert-Building> here, should work.
#2 Other Shitty Way:
Use a trigger that detect when a building is built, check if the unit-type of the constructed building is equal to your building, then check how many of that building that exists, if they are more than 5, prevent the player from building more by using
Player - Set Training/Constructing UnAvailable for player (owner of constructed building).
Have a separate trigger that detects when units are dying, check if they are a building, check if they are the building type you have and check how many of that building you have. If less than 5, enable constructing of it.
#3 The good interface way
Create some units called "Max X allowed"
go to the building you want to have a limit of max 5, set the building requirements for that building to "Max 5 allowed", you're going to need an
UNIQUE "Max X allowed" unit for each of the building types.
Then use a trigger to create the "Max 5 allowed units" for the players at map-start.
Then use a trigger that detect when a building is built, check if the unit-type of the constructed building is equal to your building, then check how many of that building that exists, if they are more than 5, remove the "Max 5 allowed" unit that is the requirement of that building.
Use a separate trigger that detects when a building is destroyed (unit - a unit is dying event), check how many buildings that the player owns, if less than 5, create 1 "Max 5 allowed" for that player.
The 3rd option is the best as the player gets a hint on why he can't build his building anymore rather than it just vanishing from the build menu suddenly.
I have the map on a separate harddrive that is in my home which I'm going back to in 15th August, if you want I can send it to you if you remember to PM me about it then.