This paper presents an extremely fast and reliable sensor-based algorithm that utilizes a combination of optimization techniques and heuristics to accomplish obstacle avoidance and path planning of mobile robots in real-time. This algorithm assumes no previous knowledge of the workspace and is capable of dealing with arbitrarily shaped obstacles with arbitrary motion characteristics. The concept of feasible circle is introduced to determine an obstacle-free subspace around the robot using information which can be obtained from distance sensors. This algorithm employs a unified approach and the modified Lagrange Multiplier method of optimization. A variable penalty, based on the distance of approach is used to keep the robot away from obstacles and the concepts of artificial obstacles and virtual obstacles are introduced to simplify the representation of the known workspace and to control the path of the robot. It is shown that the algorithm is general and can handle concave obstacles, overlapping obstacles, crowded workspaces as well as convex obstacles. The flexibility of the algorithm enables it to be easily integrated with different control strategies. The computational complexity is considerably less than for many existing algorithms, which makes the algorithm attractive for real-time applications. The algorithm is further extended to dynamic obstacles of arbitrary shapes and motion characteristics. Several case studies are presented to illustrate the capabilities of the algorithm.