Fog computing is emerging as a promising paradigm to perform distributed, low-latency computation by jointly exploiting the radio and computing resources of end-user devices and cloud servers. However, the dynamic and distributed formation of local fog networks is highly challenging due to the unpredictable arrival and departure of neighboring fog nodes. Therefore, a given fog node must properly select a set of neighboring nodes and intelligently offload its computational tasks to this set of neighboring fog nodes and the cloud in order to achieve low-latency transmission and computation. In this paper, the problem of fog network formation and task distribution is jointly investigated while considering a hybrid fog-cloud architecture. The overarching goal is to minimize the maximum communication and computation latency by enabling a given fog node to form a suitable fog network and optimize the task distribution, under uncertainty on the arrival process of neighboring fog nodes. To solve this problem, a novel online optimization framework is proposed in which the neighboring nodes are selected by using a threshold-based online algorithm that uses a target competitive ratio, defined as the ratio between the latency of the online algorithm and the offline optimal latency. The proposed framework repeatedly updates its target competitive ratio and optimizes the distribution of the fog node's computational tasks in order to minimize latency. Simulation results show that, for specific settings, the proposed framework can successfully select a set of neighboring nodes while reducing latency by up to 19.25% compared to a baseline approach based on the well-known online secretary framework. The results also show how, using the proposed framework, the computational tasks can be properly offloaded between the fog network and a remote cloud server in different network settings.