You can build on any resource that is in your territory. Once you build a city up next to it, the fence around the city will adjust to go around the resource as well.
You are still getting the benefit from the resource if it's not connected to a city.
Once it's connected to the city, your city will be able to protect it using the units stationed in the city and any city wall bonuses you might have. Also, if the city has any bonuses to food, your apiary would produce more once connected to the city.