File locking works because each user that attempts to access the same file for updating opens it for write access. The OS then locks this file to the handle that opened it first and will not allow another handle to open it for write access until it has been closed by the orignal opening handle.

With a database it actually is the same except that a databse often has different methods of handling mutliple writes to the same field. However since each user would rarely write to the same fields at the same time then that type of lock is often a non issue.