For the first problem we can stick to historical solutions, as historical siege warfare regularly involved tunneling in both attack and defense. Essentially this involves counter-mining by the defenders, this can be done in one of two ways, digging a tunnel to intersect with the attackers tunnel and trying to overcome their miners in a "battle for the tunnels" with the goal of preventing them from finishing their mine, or digging a tunnel underneath the attackers tunnel, ie "undermining" them, and then collapsing the the tunnel below in the hopes that the attackers tunnel will collapse in turn.
These solutions sound simple enough but their are a number of factors that make them difficult. the first of which is detecting where exactly the enemy tunnel is. This is an even bigger problem in a fantasy world when there are entire groups of people who live underground and may already be beneath the fortress.
In standard siege warfare it is almost always possible to recognize when mining is taking place due to the excavated earth that must be dug out. Mining costs a great deal in manpower and so is usually begun relatively close to the fortress walls allowing the defenders to realize that mining has begun even if the exact location of the mine below is not obvious. Luckily the sounds of mining can be quite loud underground and can sometimes be detected by sentries posted in cellars near the castle walls. Detecting the vibrations of enemy shafts can also be accomplished by setting out a bowl of water and observing the vibrations tin the bowl. Of course detection of mining can be made much easier by the use of magic in a fantasy setting.
Other options for detecting enemy mines involve digging test shafts under the wall in the hopes of intersecting the enemy mines, but this can often be a noisy affair underground warning the enemy that their mine is in danger and is not guaranteed to succeed. Another defense would be to dig defensive mines intended to intersect any probably places where an enemy might try to mine the walls. with these prepared mines, warriors can already be placed for defense. however this defense assumes the attackers wont undermine the defenders mines, and also that the defenders can hold the mines. if the defenders fail in the mines, that leaves a way open for the attackers to bypass the walls of the fortress altogether.
If the enemy is in the early stages of mining, the defenders might attempt to increase the width or depth of their moat, provided the fortress has one. Other defensive options once a mine is found involve introducing dangers to the miners themselves: such as flooding the mine, introducing bees into the mine, or other wild animals such as bears or wolves or attack dogs, this could include more dangerous monsters in a fantasy setting. The defenders could also release smoke or other poisons into the air to kill or drive away the attackers.
Another possible defense against mining is simply to simply construct the fortress on ground that it is impractical to tunnel in. This can vary from extremely hard stone to building over an area of sandy ground or wetland where the soil will not support a tunnel. Building near a wetland or body of water may prevent mining as the intended mine may be flooded by the high water table below ground. This isn't a guaranteed defense though as there are methods of draining or pumping the water out of these mines.
In conjunction with the above, the best defense against mining is really the construction of the fortress itself. By building the fortress with circular designs, ie round towers instead of square, the walls become harder to collapse; often the corners of fortress walls are the most vulnerable to collapse from mining. another method of defense via construction is to increase the sheer size of the walls. The larger or wider the walls are the less threat there is of a collapse. This is what happened historically with fortresses as cannons made stone walls obsolete; fortresses were built with large earthen ramparts that would not collapse in the same way as thinner stone walls.
In the case of miners from below such as goblins or dark elves many of the above defenses can be employed with modification. These attacker presumably would not have the goal of collapsing the walls of the fortress but tunneling directly inside it. Of course other methods to stop mining from below include building near water or on extremely hard rock, etc. the same would deter mining in a siege scenario. IN cases where this is not possible, defenses such as countermining, and detection of enemy mines would apply here as well. This is the case where a pre-dug series of mine galleries beneath the fortress may prove most beneficial. If tunnels are made by the defenders to stop enemies from tunneling from directly below so that they must encounter the mine galleries of the fortress first, that can slow them down or even stop them from reaching the fortress. These galleries can be constructed with chokepoints and appropriate defenses, such as caged beasts or monsters ready to be released when needed, sections of tunnel designed to be collapsed, etc. Creating these galleries in a mazelike fashion so that a someone unfamiliar with them would get lost, with secret passages for a more direct route of retreat could prove to be a common defense. Essentially we have invented the classic fantasy dungeon again but as a method of protection.
The second threat, that to the foundation of the fortress more generally is a bit harder to solve. some of it has been touched on above, building the walls to be wider so that they don't collapse as easily, but this only addresses the wall of the fortress and not the entre structure. Looking at methods of defending against earthquakes seems to be the most logical avenue of approach to this problem.
The building materials of a fortress need to have some degree of flex to withstand the lateral forces of an earthquake or shifting ground. Unreinforced masonry(brick) and stone is the least stable and can crack or totally collapse when shear forces are applied; while wood(if properly anchored) and reinforced concrete allow the building to to flex when the ground beneath shifts.
Another way to withstand the forces applied to a building during a quake is to isolate the base of the structure from the foundation. In modern buildings this done through the use of concrete columns reinforced with steel, or other methods such as the use of rubber pads. In a fantasy setting we can assume that steel reinforced concrete is out of the question, as is rubber. Instead we might assume magic could be used to accomplish the same goal; levitating the entire structure a few inches or feet off the ground. This could also be a method of deterring mining attacks. However, in this series I am looking to propose solutions that don't resort to the fantastical and could be made to work by a less technologically advanced society. As a result I don't see base isolation as a feasible solution.
Other factors can contribute to the stability of a structure. Avoiding load bearing columns on the ground floor, as well as decreasing the number of windows and doors that might weaken load bearing walls on the ground floor can make a structure more stable. Also using shear walls, ie walls that extend from the foundation to the roof, can reinforce the integrity of a structure; this precludes the stereotypical quasi-medieval building with haphazard floors in a jumble of patterns and designs that have been added over the years. Also the use of triangles in building design may prove to be more prevalent in a fantasy setting for the same reasons and triangles(and pyramidal) can withstand greater forces than other shapes under pressure. Another thing that might be counterintuitive is that taller building are more flexible than shorter ones; this is because tall buildings, if constructed of the right materials, can sway back and forth to relieve the forces applied at the base.
Another thing to keep in mind is that harder bedrock is more stable than softer or sandy soil. This may seem common sense, but with creatures that eat through rock, people may look to build on bedrock that is less prone to collapse. When the soil does collapse and the foundation of a building shifts, it might be prudent to have drains already built in to the structure in order to drain off any excess water or sewage from below ground wells, sewer systems, or septic tanks.