The practice of eating insects is called entomophagy. References using that term can include many insects. I've narrowed it down to ants and termites as best as I could, although some of the general advice and information include ants, termites, and other insects.
The short answer is that not all ants and termites are safe to eat, but most of those you're likely to encounter, either in the woods or at home, are probably edible.
That's because there are 2,000 different kinds of edible ants in the U.S., and up to 12,000 across the world. For termites, according to the National Institutes of Health-Pub Med, 45 termite species belonging to four families were recorded, and 43 of those have been used in the human diet or for feeding livestock.
To be safe, many sources say that Field Guides are strongly recommended before foraging for anything, which certainly makes sense. There are different guides to choose from, all with varying reviews. I'll leave that to you to investigate.
There are a few things to pay close attention to when choosing what to eat.
*Warning: Although many insects are edible, entomophagy poses some risks. If you are allergic to shrimp, shellfish, dust, or chocolate, never eat an insect. Even the non-allergic, unless in a survival situation, should never eat a raw insect. Certain insects store compounds that make some people sick; some are poisonous; others may be carcinogenic. Be as cautious with insects as you would be if you were gathering mushrooms. Know your insects!
Some advice is a bit vague, saying things like
Before hunting for ants, make sure you know which ones in the area are safe to eat and hunt. Source
To determine what to avoid, agreement across sources is that insects which are brightly colored, furry, or have a strong smell, should be avoided. The color can be a signal to predators that they contain poisons, which would also be toxic to us. They also tend to be the insects with stingers, including ants. Even if they're safe to eat, we can get hurt in the process.
Secrets of Survival offers a good summation:
Experts say that insects with bright colors like red, orange and yellow aren't safe to eat. Their coloration is a warning to predators that they're toxic. Due to the danger of eating poisonous insects, it's best to just swear off red, orange, and yellow bugs. On the other hand black, brown, and green are typically colors of insects that are safe to eat.
If you detect a strong odor around an insect, that's also a sign that it's probably not a safe insect to eat.
Many sources say not to eat wild insects that are already dead, especially if you're going to eat them raw. This is mostly because you won't know what killed them. If they were killed by pesticides or diseased animals, they may be carrying something that could make you sick, so it's best to steer clear. People who come across busy anthills usually trust that those ants are safe enough to give them a try. Home ant-farming is also a common way to safely incorporate ants into your diet, and is practiced all over the world for that purpose.
There's controversy over eating fire ants. Many sources classify them as edible, but since they fall into the category of insects with stingers, they're not the first choice, and to leave them alone if you have other options. If you do want to eat them, take some time to remove the stingers first. Also note that they secrete more of what gives ants their sour flavor, so some people find them less palatable. On the other hand, they're also more spicy, which is one reason people do choose them.
They're eaten all over the world, so sometimes avoidance is from people who just aren't used to them. This is an account from an American family entitled Yes we ate fire ants in a Cambodian Village! They were visiting a native family and immersing themselves in the culture, which included eating many foods not usually eaten by Americans:
Eating fire ants was not in our known realm of possibility or desire.
There we were, helping prepare lunch, and suddenly the chance to eat fire ants was in front of us. Would I have the courage to do it?
The ants were in a pile, immobilized from being in cold water. We were invited to eat them, raw and alive. Uh oh.
I can tell you firsthand the hard part is picking them up from the bowl and actually putting them in your mouth. In case you’re interested, they are a little sour at first bite.
People all over the world also eat ant larvae. They're known to be very safe and delicious. They don't have the sour taste that develops as the ant matures. You can find them in clumps in the ant nest, either at the top or close to the center of the colony, and under rocks by the edge of the nest. Sources recommend searching for them specifically because they're a delicacy unlike anything else we've tasted.
Ants can be eaten cooked or raw. Their natural taste is sour, and like vinegar, so cooking them, especially with other ingredients, makes them taste better.
According to Edible insects for humans, ants can be roasted, toasted, stir-fried, baked, used in salads, chocolate-covered or honey-buttered. The most popular recipe found on the internet using ants is called Ant Brood Tacos. If you try those tacos, report back to let us know how they were!
As for termites, which are the second most eaten insect on the planet, I couldn't find any ways to determine which are edible just be looking at them. As I said above, that's probably because most of them are.
As with ants, they can be eaten cooked or raw.
Termites can be cooked in a variety of ways: fried, steamed, roasted or boiled. Of course, if you’re on the go, you can just eat them raw.
The show Nova on Public Broadcasting Service Network interviewed a family who traveled the earth eating insects. This was taken from a discussion about eating termites in Uganda.
Peter: In Uganda, snacking on termites is like raiding the refrigerator in the U.S., except that raiding a termite mound is more work.
Faith: First, hack into a waist-high termite mound to expose tunnels; second, cover the tunnel entrances with a cloth; third, wait while soldier termites attack the invading cloth; fourth, yank away cloth, pick off insects, and eat them.
Peter: Not bad—crunchy and nutty—but the bites are too little to get a fix on the taste. This snack is not for the squeamish.
As with ants, edible termites include those found in and around the house. As long as you haven't sprayed with pest treatment or insecticide, there's nothing wrong with eating them.
In other countries termites have been valued for their nutrition and medicinal properties. There are larger species than those that I saw and they can build enormous mounds above ground. In my neck of the woods you can find termites in and under logs that lie on the ground. Pine seems to be the favored wood. They prefer warmth and are more likely to be found in sun-lit areas of the forest floor. In my case the termites were collected under mulch debris but some logs can be split open with hatchets or machetes and tapped on the side to cause the termites to fall out onto a tray or collective surface.
Next time that you’re walking in the woods, turn over branches and logs that are laying on the ground or explore a tree stump. You may find that these little critters are prevalent and plentiful, and if you dare, pluck one up and give it a taste test!
More information and recipes can be found all over the internet. Here are a few sites I liked:
Infolific Wilderness Survival
What To Do When You Have No Food
Edible bugs in the wilderness
Edible bugs that could help you survive
Secrets of Survival
Mother Earth News
Urban Survival Site