If your fence is leaning, you need to do repairs as soon as possible to avoid letting it get worse, which could damage the overall structure to the point where it must be replaced instead of repaired. Generally, most fences start leaning at the posts, which are designed to hold the fence in the ground and provide structural support to the rest of the fence.
If the posts didn't have enough concrete or were even installed without concrete, or if the concrete has cracked and become loose, you can add new concrete supports to reinforce the post. Start by digging a hole in the ground around the post down to the bottom of the existing concrete or the bottom of the post if there isn't concrete. Then, break up the concrete with a chisel and sledgehammer (always wear gloves and protective eyewear). Remove the old concrete and then fill the hole with a new concrete base.
New homes often have soft soil since the dirt had been dug up during the building process. The problem can also affect older homes in certain areas with a lot of seismic activity. As time passes, the soil will settle and the posts may start leaning as a result. This can be repaired using the same process required to fix a post with inadequate support. If your home is still new, though, you may want to wait a little while to replace the post if possible as it may shift more.
Fences should always be made with treated wood that can hold up against moisture and insects. But, even treated wood can start to rot after a while, particularly if the posts were covered with soil above the concrete, sealing moisture around the wood.
To fix this problem, you'll need to replace the posts entirely. To do this, first separate the post from the rails and slats it's attached to. You can do this by removing any slats attached directly to the post and then cutting the post above and below the rails to provide the fence with stability while you remove the rail. Then, dig a hole around the post down to the bottom of the concrete. Break up the concrete using a sledgehammer and chisel and remove the fence post.
Next, put a new post in the hole, make sure it's sitting level and then fill the hole with concrete. Finally, remove the pieces of the old post left in the fence, attach the rails to the new post and replace any removed slats.
Alternatively, the post can be cut above the rot and left in place while the bottom is removed and replaced with another piece of post. The new post can then be set level and cemented into the hole before it's attached to the old post with metal ties.