You file the flight plan for an airport that you can reach. At the planning stage you take an area around 80% along the route and choose a waypoint and create a flight plan calculation from that waypoint to the airport that you would really like to go to. When the crew get to that waypoint they see if they have the fuel required from that point to desired airport and if they do they declare their intentions and go there.
If they do not have enough fuel to do that at this point they stick to original filed plan.