Smart buildings use advanced technologies to automate building functions. One important function is occupancy detection using Internet of Things (IoT) sensors for smart buildings. Occupancy information is useful information to reduce energy consumption by automating building functions such as lighting, heating, ventilation, and air conditioning systems. The information is useful to improve indoor air quality by ensuring that ventilation systems are used only when and where they are needed. Additionally, it is useful to enhance building security by detecting unusual or unexpected occupancy levels and triggering appropriate responses, such as alarms or alerts. Occupancy information is useful for many other applications, such as emergency response, plug load energy management, point-of-interest identification, etc. However, the accuracy of occupancy detection is limited by factors such as real-time occupancy data, sensor placement, privacy concerns, and the presence of pets or objects that can interfere with sensor reading. With the rapid development of IoT sensor technologies and the increasing need for smart building solutions, there is a growing interest in occupancy detection techniques. There is a need to provide a comprehensive survey of these technologies. Although there are some exciting survey papers, they all have limited scopes with different focuses. Therefore, this paper provides a comprehensive overview of the current state-of-the-art occupancy detection methods (including both traditional algorithms and machine learning algorithms) and devices with their advantages and limitations. It surveys and compares fundamental technologies (such as sensors, algorithms, etc.) for smart buildings. Furthermore, the survey provides insights and discussions, which can help researchers, practitioners, and stakeholders develop more effective occupancy detection solutions for smart buildings.