The first line of the code is for forcing other users to log off from the database. It is impossible to change database name when someone is using it.
ALTER DATABASE OldName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
We will use sp_renamedb system stored procedure
EXEC sp_renamedb N' OldName', N'NewName'
ALTER DATABASE NewName SET MULTI_USER
There are also different ways of changing db name. First line of the above code is vital for all solutions.