Expected growth in use and implementation of wireless sensor networks (WSNs) in different environments and for different applications creates new security challenges. In WSNs, a malicious node may initiate incorrect path information, change the contents of data packets, and even hijack one or more genuine network nodes. As the network reliability completely depends on individual nodes' presence and collaborations with others, those malicious behaviors could halt WSNs. In this paper, the WSNs will be first categorized into three types in accordance to the way that data are collected. After a succinct summarization of each data gathering scheme, a comprehensive survey on security problems will be presented. Finally, some general design guidelines against typical attacks along with our proposals in WSNs will be provided. Unsolved problems and further studies will also be discussed.